"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"),label_1=require("../../../components/label/label");function Edit({attributes:e,clientId:t}){const r=(0,block_templates_1.useWooBlockProps)(e),{editEntityRecord:o,saveEntityRecord:a}=(0,data_1.useDispatch)("core"),{hasEdit:n}=(0,use_product_edits_1.useProductEdits)(),[l,i]=(0,element_1.useState)(!1),s=(0,core_data_1.useEntityId)("postType","product"),c=(0,data_1.useSelect)((e=>e("core").getEditedEntityRecord("postType","product",s))),[u,_]=(0,core_data_1.useEntityProp)("postType","product","sku"),[d,m]=(0,core_data_1.useEntityProp)("postType","product","name"),{permalinkPrefix:p,permalinkSuffix:f}=(0,data_1.useSelect)((e=>{const{getPermalinkParts:t}=e(data_2.PRODUCTS_STORE_NAME);if(s){const e=t(s);return{permalinkPrefix:null==e?void 0:e.prefix,permalinkSuffix:null==e?void 0:e.suffix}}return{}})),{ref:E,error:k,validate:q}=(0,validation_context_1.useValidation)("name",(async function(){return d&&d!==utils_1.AUTO_DRAFT_NAME?d.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")}),[d]),w=null!=k?k:s&&["publish","draft"].includes(c.status)&&p&&(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:c.permalink,target:"_blank",rel:"noreferrer"},p,c.slug||(0,url_1.cleanForSlug)(d),f),(0,element_1.createElement)(components_1.Button,{variant:"link",onClick:()=>i(!0)},(0,i18n_1.__)("Edit","woocommerce"))),x=(0,compose_1.useInstanceId)(components_1.BaseControl,"product_name"),{selectBlock:g}=(0,data_1.useDispatch)("core/block-editor");return(0,element_1.useEffect)((()=>{e.autoFocus&&g(t)}),[]),(0,element_1.createElement)(element_1.Fragment,null,(0,element_1.createElement)("div",{...r},(0,element_1.createElement)(components_1.BaseControl,{id:x,label:(0,element_1.createElement)(label_1.Label,{label:(0,i18n_1.__)("Name","woocommerce"),required:!0}),className:(0,classnames_1.default)({"has-error":k}),help:w},(0,element_1.createElement)(components_1.__experimentalInputControl,{id:x,ref:E,name:"name",autoFocus:e.autoFocus,placeholder:(0,i18n_1.__)("e.g. 12 oz Coffee Mug","woocommerce"),onChange:m,value:d&&d!==utils_1.AUTO_DRAFT_NAME?d:"",autoComplete:"off","data-1p-ignore":!0,onBlur:()=>{n("name")&&(u||k||_((0,url_1.cleanForSlug)(d)),q())}})),l&&(0,element_1.createElement)(edit_product_link_modal_1.EditProductLinkModal,{permalinkPrefix:p||"",permalinkSuffix:f||"",product:c,onCancel:()=>i(!1),onSaved:()=>i(!1),saveHandler:async e=>{const{slug:t,permalink:r}=await a("postType","product",{id:c.id,slug:e});if(t&&r)return o("postType","product",c.id,{slug:t,permalink:r}),{slug:t,permalink:r}}})))}exports.Edit=Edit;