import{createElement as e}from"../../../../../../external/preact/dist/preact.js";import{useState as l,useMemo as n,useEffect as t}from"../../../../../../external/preact/hooks/dist/hooks.js";import a from"../../../../../internal/FormFields/Field/Field.js";import{useCoreContext as s}from"../../../../../../core/Context/CoreProvider.js";import i from"../../../../../internal/FormFields/Fieldset/Fieldset.js";import o from"../../../../../internal/FormFields/RadioGroup/RadioGroup.js";import m from"../../../../../internal/FormFields/Select/Select.js";import{alternativeLabelContent as r}from"../FieldLabelAlternative.js";import{useAmount as u}from"../../../../../../core/Context/AmountProvider.js";function d(d){const{i18n:v}=s(),{amount:c}=u(),{brand:p,onChange:b,type:g}=d,f=d.installmentOptions[p]||d.installmentOptions.card,h=1===f?.values?.length,[F,C]=l(f?.preselectedValue||f?.values[0]),[j,x]=l("onetime"),N=f?.plans?.includes("revolving")||f?.plans?.includes("bonus"),_=n(()=>function(e,l){return e?[{id:"onetime",name:"installments.oneTime"},{id:"installments",name:"installments.installments"},...l?.includes("revolving")?[{id:"revolving",name:"installments.revolving"}]:[],...l?.includes("bonus")?[{id:"bonus",name:"installments.bonus"}]:[]]:[]}(N,f?.plans),[N,f]),E=e=>{const l=e.target.value;C(Number(l))},y=e=>{const l=e.currentTarget.getAttribute("value");x(l)},L=e=>{let l,n;if("amount"===g){l="installmentOption",n={count:e,values:{times:e,partialValue:(e=>v.amount(c.value/e,c.currency))(e)}}}else l="installmentOptionMonths",n={count:e,values:{times:e}};return{id:e,name:c.value?v.get(l,n):`${e}`}};return t(()=>{f?.values?.includes(F)||C(f?.preselectedValue??f?.values[0])},[p]),t(()=>{const e={value:F,...N&&"onetime"===j&&{value:1},...N&&"revolving"===j&&{value:1,plan:"revolving"},...N&&"bonus"===j&&{value:1,plan:"bonus"}};b(f?e:{value:null})},[b,N,F,f,j]),f&&c&&0!==c.value?N?e("div",{className:"adyen-checkout__installments adyen-checkout__installments--revolving-plan"},e(a,{label:v.get("installments"),classNameModifiers:["installments"],name:"installmentsPseudoLabel",useLabelElement:!1,showContextualElement:!1,renderAlternativeToLabel:r},e(i,{classNameModifiers:["revolving-plan"],label:""},e(o,{items:_,onChange:y,value:j,ariaLabel:v.get("installments")}),e(a,{className:"installments"!==j?"revolving-plan-installments__disabled":"revolving-plan-installments",classNameModifiers:["revolving-plan-installments"],name:"",useLabelElement:!1,showContextualElement:!1},e(m,{filterable:!1,items:f.values.map(L),selectedValue:F,onChange:E,name:"installments",disabled:"installments"!==j}))))):e("div",{className:"adyen-checkout__installments"},e(a,{label:v.get("installments"),classNameModifiers:["installments"],name:"installments",showContextualElement:!1},e(m,{filterable:!1,items:f.values.map(L),selectedValue:F,onChange:E,name:"installments",readonly:h,disabled:h}))):null}d.defaultProps={brand:"",onChange:()=>{}};export{d as default};
//# sourceMappingURL=Installments.js.map
