"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Edit=void 0;const i18n_1=require("@wordpress/i18n"),data_1=require("@woocommerce/data"),block_editor_1=require("@wordpress/block-editor"),tracks_1=require("@woocommerce/tracks"),element_1=require("@wordpress/element"),data_2=require("@wordpress/data"),core_data_1=require("@wordpress/core-data"),variations_table_1=require("../../components/variations-table"),validation_context_1=require("../../contexts/validation-context"),variable_product_tour_1=require("./variable-product-tour"),constants_1=require("../../constants"),handle_prompt_1=require("../../utils/handle-prompt");function Edit({context:e}){const t=(0,element_1.useRef)(!1),{invalidateResolution:r}=(0,data_2.useDispatch)(data_1.EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME),o=(0,core_data_1.useEntityId)("postType","product"),i=(0,block_editor_1.useBlockProps)(),[a]=(0,core_data_1.useEntityProp)("postType","product","status"),s=(0,element_1.useMemo)((()=>({product_id:o,order:"asc",orderby:"menu_order",has_price:!1})),[o]),{totalCountWithoutPrice:_}=(0,data_2.useSelect)((e=>{const{getProductVariationsTotalCount:t}=e(data_1.EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME);return{totalCountWithoutPrice:t(s)}}),[o]),{updateUserPreferences:n,variable_items_without_price_notice_dismissed:c}=(0,data_1.useUserPreferences)(),{ref:d}=(0,validation_context_1.useValidation)("variations",(async function(e,r){if(_>0&&!t.current&&"publish"!==a&&"publish"===(null==r?void 0:r.status))return"yes"!==c&&n({variable_items_without_price_notice_dismissed:{...c||{},[o]:"no"}}),(0,i18n_1.__)("Set variation prices before adding this product.","woocommerce")}),[_]),u=!c||"yes"!==c[o],l=_>0&&u?(0,i18n_1.sprintf)((0,i18n_1.__)("%d variations do not have prices. Variations that do not have prices will not be visible to customers.","woocommerce"),_):"";return(0,element_1.createElement)("div",{...i},(0,element_1.createElement)(variations_table_1.VariationsTable,{ref:d,noticeText:l,onNoticeDismiss:()=>{t.current=!0,n({variable_items_without_price_notice_dismissed:{...c||{},[o]:"yes"}})},noticeActions:[{label:(0,i18n_1.__)("Set prices","woocommerce"),onClick:function(e){(0,tracks_1.recordEvent)("product_variations_set_prices_select",{source:constants_1.TRACKS_SOURCE});const t=(0,data_2.resolveSelect)(data_1.EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME).getProductVariations({product_id:o,has_price:!1,_fields:["id"]});(0,handle_prompt_1.handlePrompt)({onOk(r){(0,tracks_1.recordEvent)("product_variations_set_prices_update",{source:constants_1.TRACKS_SOURCE}),t.then((t=>{e(t.map((({id:e})=>({id:e,regular_price:r}))))}))}})},className:"is-destructive"}],onVariationTableChange:(e,t)=>{("delete"===e||"update"===e&&t&&t.find((e=>e.regular_price||e.sale_price)))&&r("getProductVariationsTotalCount",[s])}}),(null==e?void 0:e.isInSelectedTab)&&(0,element_1.createElement)(variable_product_tour_1.VariableProductTour,null))}exports.Edit=Edit;