"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Edit=void 0;const i18n_1=require("@wordpress/i18n"),data_1=require("@woocommerce/data"),block_templates_1=require("@woocommerce/block-templates"),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({attributes:e,context:t}){const r=(0,element_1.useRef)(!1),{invalidateResolution:o}=(0,data_2.useDispatch)(data_1.EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME),i=(0,core_data_1.useEntityId)("postType","product"),a=(0,block_templates_1.useWooBlockProps)(e),[s]=(0,core_data_1.useEntityProp)("postType","product","status"),_=(0,element_1.useMemo)((()=>({product_id:i,order:"asc",orderby:"menu_order",has_price:!1})),[i]),{totalCountWithoutPrice:n}=(0,data_2.useSelect)((e=>{const{getProductVariationsTotalCount:t}=e(data_1.EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME);return{totalCountWithoutPrice:t(_)}}),[_]),{updateUserPreferences:c,variable_items_without_price_notice_dismissed:d}=(0,data_1.useUserPreferences)(),{ref:u}=(0,validation_context_1.useValidation)("variations",(async function(e,t){if(n>0&&!r.current&&"publish"!==s&&"publish"===(null==t?void 0:t.status))return"yes"!==d&&c({variable_items_without_price_notice_dismissed:{...d||{},[i]:"no"}}),(0,i18n_1.__)("Set variation prices before adding this product.","woocommerce")}),[n]),l=!d||"yes"!==d[i],p=n>0&&l?(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"),n):"";return(0,element_1.createElement)("div",{...a},(0,element_1.createElement)(variations_table_1.VariationsTable,{ref:u,noticeText:p,onNoticeDismiss:()=>{r.current=!0,c({variable_items_without_price_notice_dismissed:{...d||{},[i]:"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:i,order:"asc",orderby:"menu_order",has_price:!1,_fields:["id"],per_page:n});(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=>"regular_price"in e||"sale_price"in e)))&&o("getProductVariationsTotalCount",[_])}}),t.isInSelectedTab&&(0,element_1.createElement)(variable_product_tour_1.VariableProductTour,null))}exports.Edit=Edit;