"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var jsxRuntime=require("react/jsx-runtime"),core=require("@gem-sdk/core"),React=require("react"),ProductPropertyChoice=require("./property-choice/ProductPropertyChoice.js"),ProductPropertyInput=require("./property-input/ProductPropertyInput.js"),ProductPropertySelect=require("./property-select/ProductPropertySelect.js"),system=require("@gem-sdk/system"),helpers=require("./common/helpers.js"),styles=require("./common/styles.js"),attrs=require("./common/attrs.js"),classes=require("./common/classes.js");const ProductProperties=({setting:e,styles:t,builderProps:r})=>{let{name:s,label:a,type:l,required:u,minChar:c,maxChar:o,numberOnly:i,minNumber:d,maxNumber:n,childItem:m,requiredMessage:p,invalidEmailMessage:y,invalidLengthMessage:g,invalidNumberMessage:x}=e??{},{isDisplayLabel:j=!0}=t??{},P=core.useUniqProductID(),h=core.useProductStore(e=>e.updateProperty),v=core.useProductStore(e=>e.addProperty),R=core.useProductStore(e=>e.isSubmit),[C,f]=React.useState(""),[b,S]=React.useState(!1),[k,w]=React.useState([]),q=core.usePrevious(s),N=core.useProduct(),I=React.useRef(null),{trigger:M}=core.useInteraction(),E=React.useCallback((e,t)=>{M({event:"gp:rollback:option-selected",selector:`[data-id="${r?.uid}"`,element:t}),M({event:"gp:option-selected",data:e,selector:`[data-id="${r?.uid}"`,element:t})},[r?.uid,M]),L=React.useCallback(e=>{if(window.interactionLoaded){e();return}window.addEventListener("gp:flow-actions-loaded",()=>{e()})},[]);React.useEffect(()=>{if("dropdown"===l){let e=I.current?.querySelector("select")?.value;L(()=>{E(e,I.current)})}},[E,L,l]),React.useEffect(()=>{s&&(q?h(q,{key:s,value:C,required:u}):v({key:s,value:"",required:u}))},[v,q,h,s,u,C]);let $=React.useCallback(e=>{let t=e?.length??0;return!u&&0===t||(i?(Number(d)||0)<=Number(e)&&Number(t)<=(Number(n)||0):(c??0)<=t&&t<=(o??0))},[o,n,c,d,i,u]),W=React.useCallback(e=>u&&!e?{message:p}:i&&e&&!/^\d+$/.test(e)?{message:x}:$(e)?void 0:{message:g},[$,i,u,p,g,x]),V=React.useCallback(e=>{if(!e?.trim()||!core.validateEmail(e))return{message:y}},[y]),Z=React.useCallback(e=>{if(!e?.trim()&&u)return{message:p}},[u,p]),A=React.useCallback(e=>{if(u&&(!e||e.length<=0))return{message:p}},[u,p]),D=e=>{if(S(!0),"checkbox"===l){let t=[];w(t=e.target.checked?[...k,e.target.value]:k.filter(t=>t!==e.target.value)),E(t,I.current),h(e.target.name,{value:t.join(", ")})}else f(e.target.value),E(e.target.value,I.current),h(e.target.name,{value:e.target.value})},_=e=>{S(!0),f(e.target.value),E(e.target.value,I.current),h(e.target.name,{value:e.target.value})},B=React.useMemo(()=>{let e=()=>{switch(l){case"email":return V(C);case"checkbox":return A(k);case"radio":case"dropdown":return Z(C);default:return W(C)}},t=e()?.message,r=void 0!==C&&""!==C,s=!R&&!b||!t;return{isValid:s,isRequired:r,message:t}},[C,R,b,l,V,A,k,Z,W]),F=N?.baseID?.replace("gid://shopify/Product/",""),O=system.createAttr({...attrs.getWrapperTSXAttrs(r?.uid,F)}),T=system.createStyle({...styles.getWrapperStyles(t)}),U=system.createStyle({...styles.getLabelStyles(t)}),X=system.createClass({...classes.getLabelClasses(t)}),z=system.createStyle({...styles.getLabelContentStyles()}),G=helpers.getLabelContent(e),H=system.createClass({...classes.getWrapperErrorMessageClasses(),"gp-hidden":B.isValid}),J=system.createStyle({...styles.getWrapperErrorMessageStyles(t)});return jsxRuntime.jsxs("div",{...O,style:T,id:`shopify-text-element-${r?.uid}`,ref:I,children:[j&&jsxRuntime.jsx("div",{children:jsxRuntime.jsxs("label",{htmlFor:`${P}-${s}`,className:X,style:U,children:[a,jsxRuntime.jsx("span",{style:z,children:G})]})}),jsxRuntime.jsxs("div",{children:["dropdown"===l&&jsxRuntime.jsx(ProductPropertySelect.default,{setting:{...e,handleChangeOption:_},styles:t}),("checkbox"===l||"radio"===l)&&jsxRuntime.jsx(ProductPropertyChoice.default,{setting:{...e,childItem:m,name:s,handleChange:D},currentProductId:F,styles:t,defaultName:r?.uid}),("text"===l||"textarea"===l||"email"===l||"hidden"===l)&&jsxRuntime.jsx(ProductPropertyInput.default,{setting:{...e,handleChange:D,validationData:B},styles:t}),jsxRuntime.jsxs("div",{className:H,style:J,children:[jsxRuntime.jsx("svg",{width:"16",height:"16",viewBox:"0 0 16 16",fill:"currentColor",xmlns:"http://www.w3.org/2000/svg",children:jsxRuntime.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"})}),jsxRuntime.jsx("span",{className:"gp-pl-1",children:B.message})]})]})]})};exports.default=ProductProperties;
