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