"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react/jsx-runtime"),t=require("@gem-sdk/core"),r=require("react"),s=require("./property-choice/ProductPropertyChoice.js"),a=require("./property-input/ProductPropertyInput.js"),l=require("./property-select/ProductPropertySelect.js"),u=require("@gem-sdk/system"),i=require("./common/helpers.js"),c=require("./common/styles.js"),d=require("./common/attrs.js"),o=require("./common/classes.js");exports.default=({setting:n,styles:g,builderProps:p})=>{let{name:m,label:y,type:v,required:h,minChar:C,maxChar:f,numberOnly:b,minNumber:x,maxNumber:j,childItem:S,requiredMessage:P,invalidEmailMessage:k,invalidLengthMessage:w,invalidNumberMessage:q}=n??{},{isDisplayLabel:N=!0}=g??{},M=t.useUniqProductID(),E=t.useProductStore(e=>e.updateProperty),L=t.useProductStore(e=>e.addProperty),$=t.useProductStore(e=>e.isSubmit),[I,W]=r.useState(""),[R,V]=r.useState(!1),[Z,A]=r.useState([]),D=t.usePrevious(m),O=t.useProduct(),_=r.useRef(null),{trigger:B}=t.useInteraction(),F=r.useCallback((e,t)=>{B({event:"gp:rollback:option-selected",selector:`[data-id="${p?.uid}"`,element:t}),B({event:"gp:option-selected",data:e,selector:`[data-id="${p?.uid}"`,element:t})},[p?.uid,B]),T=r.useCallback(e=>{if(window.interactionLoaded){e();return}window.addEventListener("gp:flow-actions-loaded",()=>{e()})},[]);r.useEffect(()=>{if("dropdown"===v){let e=_.current?.querySelector("select")?.value;T(()=>{F(e,_.current)})}},[F,T,v]),r.useEffect(()=>{m&&(D?E(D,{key:m,value:I,required:h}):L({key:m,value:"",required:h}))},[L,D,E,m,h,I]);let U=r.useCallback(e=>{let t=e?.length??0;return!h&&0===t||(b?(Number(x)||0)<=Number(e)&&Number(t)<=(Number(j)||0):(C??0)<=t&&t<=(f??0))},[f,j,C,x,b,h]),X=r.useCallback(e=>h&&!e?{message:P}:b&&e&&!/^\d+$/.test(e)?{message:q}:U(e)?void 0:{message:w},[U,b,h,P,w,q]),z=r.useCallback(e=>{if(!e?.trim()||!t.validateEmail(e))return{message:k}},[k]),G=r.useCallback(e=>{if(!e?.trim()&&h)return{message:P}},[h,P]),H=r.useCallback(e=>{if(h&&(!e||e.length<=0))return{message:P}},[h,P]),J=e=>{if(V(!0),"checkbox"===v){let t=[];A(t=e.target.checked?[...Z,e.target.value]:Z.filter(t=>t!==e.target.value)),F(t,_.current),E(e.target.name,{value:t.join(", ")})}else W(e.target.value),F(e.target.value,_.current),E(e.target.name,{value:e.target.value})},K=r.useMemo(()=>{let e=(()=>{switch(v){case"email":return z(I);case"checkbox":return H(Z);case"radio":case"dropdown":return G(I);default:return X(I)}})()?.message,t=void 0!==I&&""!==I,r=!$&&!R||!e;return{isValid:r,isRequired:t,message:e}},[I,$,R,v,z,H,Z,G,X]),Q=O?.baseID?.replace("gid://shopify/Product/",""),Y=u.createAttr({...d.getWrapperTSXAttrs(p?.uid,Q)}),ee=u.createStyle({...c.getWrapperStyles(g)}),et=u.createStyle({...c.getLabelStyles(g)}),er=u.createClass({...o.getLabelClasses(g)}),es=u.createStyle({...c.getLabelContentStyles()}),ea=i.getLabelContent(n),el=u.createClass({...o.getWrapperErrorMessageClasses(),"gp-hidden":K.isValid}),eu=u.createStyle({...c.getWrapperErrorMessageStyles(g)});return e.jsxs("div",{...Y,style:ee,id:`shopify-text-element-${p?.uid}`,ref:_,children:[N&&e.jsx("div",{children:e.jsxs("label",{htmlFor:`${M}-${m}`,className:er,style:et,children:[y,e.jsx("span",{style:es,children:ea})]})}),e.jsxs("div",{children:["dropdown"===v&&e.jsx(l.default,{setting:{...n,handleChangeOption:e=>{V(!0),W(e.target.value),F(e.target.value,_.current),E(e.target.name,{value:e.target.value})}},styles:g}),("checkbox"===v||"radio"===v)&&e.jsx(s.default,{setting:{...n,childItem:S,name:m,handleChange:J},currentProductId:Q,styles:g,defaultName:p?.uid}),("text"===v||"textarea"===v||"email"===v||"hidden"===v)&&e.jsx(a.default,{setting:{...n,handleChange:J,validationData:K},styles:g}),e.jsxs("div",{className:el,style:eu,children:[e.jsx("svg",{width:"16",height:"16",viewBox:"0 0 16 16",fill:"currentColor",xmlns:"http://www.w3.org/2000/svg",children:e.jsx("path",{fillRule:"evenodd",clipRule:"evenodd",d:"M15 8C15 11.866 11.866 15 8 15C4.13401 15 1 11.866 1 8C1 4.13401 4.13401 1 8 1C11.866 1 15 4.13401 15 8ZM8 10.5C7.58579 10.5 7.25 10.8358 7.25 11.25C7.25 11.6642 7.58579 12 8 12C8.41421 12 8.75 11.6642 8.75 11.25C8.75 10.8358 8.41421 10.5 8 10.5ZM8.5 4C8.5 3.72386 8.27614 3.5 8 3.5C7.72386 3.5 7.5 3.72386 7.5 4V9C7.5 9.27614 7.72386 9.5 8 9.5C8.27614 9.5 8.5 9.27614 8.5 9V4Z",fill:"currentColor"})}),e.jsx("span",{className:"gp-pl-1",children:K.message})]})]})]})};
