"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"),components_2=require("@woocommerce/components"),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("./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");function getFirstOptionFromEachAttribute(e){return e.map((e=>({id:e.id,name:e.name,option:e.options[0]})))}function Edit({attributes:e}){const{description:t}=e,{generateProductVariations:r}=(0,use_product_variations_helper_1.useProductVariationsHelper)(),[o,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:l}=(0,use_product_attributes_1.useProductAttributes)({allAttributes:a,isVariationAttributes:!0,productId:(0,core_data_1.useEntityId)("postType","product"),onChange(e){s(e),n(getFirstOptionFromEachAttribute(e)),r(e)}}),u=(0,utils_2.hasAttributesUsedForVariations)(a),_=(0,block_editor_1.useBlockProps)({className:(0,classnames_1.default)({"wp-block-woocommerce-product-variations-fields--has-attributes":u})}),d=(0,block_editor_1.useInnerBlocksProps)({className:"wp-block-woocommerce-product-variations-fields__content"},{templateLock:"all"}),m=()=>{i(!1)};return(0,element_1.createElement)("div",{..._},(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,i18n_1.__)("Add variation options","woocommerce")))),(0,element_1.createElement)("div",{...d}),o&&(0,element_1.createElement)(new_attribute_modal_1.NewAttributeModal,{title:(0,i18n_1.__)("Add variation options","woocommerce"),description:(0,element_1.createInterpolateElement)((0,i18n_1.__)("Select from existing <globalAttributeLink>global attributes</globalAttributeLink> or create options for buyers to choose on the product page. You can change the order later.","woocommerce"),{globalAttributeLink:(0,element_1.createElement)(components_2.Link,{href:"https://woocommerce.com/document/variable-product/#add-attributes-to-use-for-variations",type:"external",target:"_blank"})}),createNewAttributesAsGlobal:!0,notice:"",onCancel:()=>{m()},onAdd:e=>{l([...e.filter((e=>!c.find((t=>(0,utils_1.getAttributeId)(e)===(0,utils_1.getAttributeId)(t)))))]),m()},selectedAttributeIds:c.map((e=>e.id)),disabledAttributeIds:a.filter((e=>!e.variation)).map((e=>e.id))}))}exports.Edit=Edit;