"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 i18n_1=require("@wordpress/i18n"),element_1=require("@wordpress/element"),compose_1=require("@wordpress/compose"),url_1=require("@wordpress/url"),data_1=require("@wordpress/data"),block_templates_1=require("@woocommerce/block-templates"),data_2=require("@woocommerce/data"),classnames_1=__importDefault(require("classnames")),components_1=require("@wordpress/components"),core_data_1=require("@wordpress/core-data"),utils_1=require("../../../utils"),edit_product_link_modal_1=require("../../../components/edit-product-link-modal"),validation_context_1=require("../../../contexts/validation-context"),use_product_edits_1=require("../../../hooks/use-product-edits");function Edit({attributes:e}){const t=(0,block_templates_1.useWooBlockProps)(e),{editEntityRecord:r,saveEntityRecord:o}=(0,data_1.useDispatch)("core"),{hasEdit:n}=(0,use_product_edits_1.useProductEdits)(),[a,i]=(0,element_1.useState)(!1),l=(0,core_data_1.useEntityId)("postType","product"),s=(0,data_1.useSelect)((e=>e("core").getEditedEntityRecord("postType","product",l))),[c,_]=(0,core_data_1.useEntityProp)("postType","product","sku"),[u,d]=(0,core_data_1.useEntityProp)("postType","product","name"),{permalinkPrefix:m,permalinkSuffix:p}=(0,data_1.useSelect)((e=>{const{getPermalinkParts:t}=e(data_2.PRODUCTS_STORE_NAME);if(l){const e=t(l);return{permalinkPrefix:null==e?void 0:e.prefix,permalinkSuffix:null==e?void 0:e.suffix}}return{}})),{ref:f,error:E,validate:k}=(0,validation_context_1.useValidation)("name",(async function(){return u&&u!==utils_1.AUTO_DRAFT_NAME?u.length>120?(0,i18n_1.__)("Please enter a product name shorter than 120 characters.","woocommerce"):void 0:(0,i18n_1.__)("Name field is required.","woocommerce")}),[u]),w=null!=E?E:l&&["publish","draft"].includes(s.status)&&m&&(0,element_1.createElement)("span",{className:"woocommerce-product-form__secondary-text product-details-section__product-link"},(0,i18n_1.__)("Product link","woocommerce"),": ",(0,element_1.createElement)("a",{href:s.permalink,target:"_blank",rel:"noreferrer"},m,s.slug||(0,url_1.cleanForSlug)(u),p),(0,element_1.createElement)(components_1.Button,{variant:"link",onClick:()=>i(!0)},(0,i18n_1.__)("Edit","woocommerce"))),q=(0,compose_1.useInstanceId)(components_1.BaseControl,"product_name");return(0,element_1.createElement)(element_1.Fragment,null,(0,element_1.createElement)("div",{...t},(0,element_1.createElement)(components_1.BaseControl,{id:q,label:(0,element_1.createInterpolateElement)((0,i18n_1.__)("Name <required />","woocommerce"),{required:(0,element_1.createElement)("span",{className:"woocommerce-product-form__required-input"},(0,i18n_1.__)("*","woocommerce"))}),className:(0,classnames_1.default)({"has-error":E}),help:w},(0,element_1.createElement)(components_1.__experimentalInputControl,{id:q,ref:f,name:"name",autoFocus:e.autoFocus,placeholder:(0,i18n_1.__)("e.g. 12 oz Coffee Mug","woocommerce"),onChange:d,value:u&&u!==utils_1.AUTO_DRAFT_NAME?u:"",autoComplete:"off","data-1p-ignore":!0,onBlur:()=>{n("name")&&(c||E||_((0,url_1.cleanForSlug)(u)),k())}})),a&&(0,element_1.createElement)(edit_product_link_modal_1.EditProductLinkModal,{permalinkPrefix:m||"",permalinkSuffix:p||"",product:s,onCancel:()=>i(!1),onSaved:()=>i(!1),saveHandler:async e=>{const{slug:t,permalink:n}=await o("postType","product",{id:s.id,slug:e});if(t&&n)return r("postType","product",s.id,{slug:t,permalink:n}),{slug:t,permalink:n}}})))}exports.Edit=Edit;