"use strict";var e=require("react"),t=require("@gem-sdk/core"),l=require("../common/extractSellingPlanSettings.js"),n=require("../helpers/findMatchedGroup.js"),r=require("../helpers/findDefaultValue.js"),u=require("../helpers/findVariantByOptions.js");exports.useSellingSubscription=s=>{let i=t.useProduct(),a=t.useVariants(),{selectedOptions:o}=t.useSelectedOption(),d=t.useProductStore(e=>e.selectedSellingPlan),c=t.useProductStore(e=>e.setSelectedSellingPlan),f=t.usePageStore(e=>e.sellingPlanMeta),p=t.useEditorMode(),g=e.useRef(!1),P=e.useRef(void 0);e.useEffect(()=>{if(!g.current&&p===t.RenderModeEnv.EDIT&&!f){let e=new CustomEvent("element-subscription-mount");globalThis.dispatchEvent(e),g.current=!0}},[p,f]);let S=f??s?.sellingPlans,m=e.useMemo(()=>{if(!S)return{sourceApp:void 0,group:void 0,defaultOptions:void 0};let e=f&&o?u.findVariantByOptions({variants:a,selectedOptions:o}):null,t=l.extractSellingPlanSettings({sellingPlansSetting:S,currentVariant:e,product:i,isFromEvent:!!f});return t},[S,f,a,o,i]),v=e.useMemo(()=>o?u.findVariantByOptions({variants:a,selectedOptions:o}):null,[o,a]);e.useEffect(()=>{P.current=void 0},[v?.baseID]);let{sellingPlans:E,defaultValue:V}=e.useMemo(()=>{let e=v?.sellingPlanGroups??i?.sellingPlanGroups??[];if(!e||0===e.length)return{sellingPlans:[],defaultValue:void 0};let t=n.findMatchedGroup({sellingPlanGroups:e,extractedSettings:m,currentVariant:v}),l=t?.sellingPlans?.filter(e=>null!=e)??[],u=l.map(e=>({label:e.name||"",value:e.baseID})),s=r.findDefaultValue({validPlans:l,extractedSettings:m,currentVariant:v,plans:u});return{sellingPlans:u,defaultValue:s}},[i,v,m]);return e.useEffect(()=>{!d&&V&&P.current!==V&&(P.current=V,c(V))},[V,d,c]),{sellingPlans:E,defaultValue:V,selectedSellingPlan:d,setSelectedSellingPlan:c}};
