"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Edit=void 0;const classnames_1=__importDefault(require("classnames")),components_1=require("@wordpress/components"),block_templates_1=require("@woocommerce/block-templates"),tracks_1=require("@woocommerce/tracks"),element_1=require("@wordpress/element"),i18n_1=require("@wordpress/i18n"),block_editor_1=require("@wordpress/block-editor"),core_data_1=require("@wordpress/core-data"),sanitize_html_1=require("../../../utils/sanitize-html"),empty_variations_image_1=require("../../../images/empty-variations-image"),new_attribute_modal_1=require("../../../components/attribute-control/new-attribute-modal"),use_product_attributes_1=require("../../../hooks/use-product-attributes"),utils_1=require("../../../components/attribute-control/utils"),use_product_variations_helper_1=require("../../../hooks/use-product-variations-helper"),utils_2=require("../../../utils"),constants_1=require("../../../constants");function Edit({attributes:e}){const{description:t}=e,{generateProductVariations:o}=(0,use_product_variations_helper_1.useProductVariationsHelper)(),[r,i]=(0,element_1.useState)(!1),[a,s]=(0,core_data_1.useEntityProp)("postType","product","attributes"),[,n]=(0,core_data_1.useEntityProp)("postType","product","default_attributes"),{attributes:c,handleChange:_}=(0,use_product_attributes_1.useProductAttributes)({allAttributes:a,isVariationAttributes:!0,productId:(0,core_data_1.useEntityId)("postType","product"),onChange(e,t){s(e),n(t),o(e,t)}}),d=(0,utils_2.hasAttributesUsedForVariations)(a),l=(0,block_templates_1.useWooBlockProps)(e,{className:(0,classnames_1.default)({"wp-block-woocommerce-product-variations-fields--has-attributes":d})}),u=(0,block_editor_1.useInnerBlocksProps)({className:"wp-block-woocommerce-product-variations-fields__content"},{templateLock:"all"}),m=()=>{i(!1)};return(0,element_1.createElement)("div",{...l},(0,element_1.createElement)("div",{className:"wp-block-woocommerce-product-variations-fields__heading"},(0,element_1.createElement)("div",{className:"wp-block-woocommerce-product-variations-fields__heading-image-container"},(0,element_1.createElement)(empty_variations_image_1.EmptyVariationsImage,null)),(0,element_1.createElement)("p",{className:"wp-block-woocommerce-product-variations-fields__heading-description",dangerouslySetInnerHTML:(0,sanitize_html_1.sanitizeHTML)(t)}),(0,element_1.createElement)("div",{className:"wp-block-woocommerce-product-variations-fields__heading-actions"},(0,element_1.createElement)(components_1.Button,{variant:"primary",onClick:()=>{i(!0),(0,tracks_1.recordEvent)("product_options_add_first_option")}},(0,i18n_1.__)("Add variation options","woocommerce")))),(0,element_1.createElement)("div",{...u}),r&&(0,element_1.createElement)(new_attribute_modal_1.NewAttributeModal,{title:(0,i18n_1.__)("Add variation options","woocommerce"),description:(0,i18n_1.__)("Select from existing attributes or create new ones to add new variations for your product. You can change the order later.","woocommerce"),createNewAttributesAsGlobal:!0,notice:"",onCancel:()=>{(0,tracks_1.recordEvent)("product_options_modal_cancel_button_click"),m()},onAdd:e=>{const t=e.filter((e=>!c.some((t=>(0,utils_1.getAttributeId)(e)===(0,utils_1.getAttributeId)(t)))));(0,tracks_1.recordEvent)("product_options_add",{source:constants_1.TRACKS_SOURCE,options:t.map((e=>({attribute:e.name,values:e.options})))}),_(t),m()},onAddAnother:()=>{(0,tracks_1.recordEvent)("product_add_options_modal_add_another_option_button_click")},onRemoveItem:()=>{(0,tracks_1.recordEvent)("product_add_options_modal_remove_option_button_click")},selectedAttributeIds:c.map((e=>e.id)),disabledAttributeIds:a.filter((e=>!e.variation)).map((e=>e.id)),termsAutoSelection:"all"}))}exports.Edit=Edit;