import{r as d,j as e}from"./vendor-react-wBh_31cN.js";import{a as I,b as w,c as F}from"./index.esm-CAGtek53.js";import{I as g}from"./icon-button-FETOk6q1.js";import{a as C,I as R}from"./input-H6_qmzms.js";import{L as A}from"./label-Dnebucx5.js";import{q as L,R as B}from"./index-BKr2rJhV.js";import{g as E}from"./getInputValidation-CUi6Oel7.js";import{az as V,Y as S}from"./vendor-icons-BUUmD6ht.js";const q=({name:t,fields:s,required:p,onClear:j,noDeleteOnFirstRow:y=!1,isDisabled:r=!1,withLabelOnFirstRow:x=!1,className:o})=>{const{register:b,control:f,formState:{errors:c}}=I(),{fields:u,append:i,remove:h}=F({control:f,name:t,rules:{minLength:p?1:0}}),v=d.useCallback(()=>i(s.reduce((n,a)=>({...n,[a.name]:a.defaultValue}),{})),[i,s]),m=E(t,c);return e.jsxs("div",{className:o,children:[m.info&&e.jsx(C,{intent:m.intent,children:m.info}),u.map((n,a)=>{var k;const $=((k=c==null?void 0:c[t])==null?void 0:k[a])??{};return e.jsxs("div",{className:"mb-2 flex items-start",children:[s.map(l=>e.jsxs("div",{className:"mr-4 flex-1",children:[x&&a===0&&e.jsx(A,{className:"mb-1",children:l.name}),e.jsx(R,{...b(`${t}.${a}.${l.name}`),disabled:r,placeholder:l.placeholder,defaultValue:n[l.name],type:l.type}),(()=>{const N=E(l.name,$);return N.info?e.jsx(C,{intent:N.intent,children:N.info}):null})()]},l.name)),e.jsx(g,{type:"button",variant:"outline",disabled:r||y&&u.length<=1,"aria-label":"Remove",onClick:()=>{u.length===1?j():h(a)},className:x&&a===0?"mt-[23px]":void 0,children:e.jsx(S,{className:"w-4 h-4"})})]},n.id)}),e.jsx(g,{type:"button",variant:"outline",onClick:v,disabled:r,"aria-label":"Add","data-test":`${t}-1-plus-button`,children:e.jsx(V,{className:"w-4 h-4"})},"append-not-empty")]})},z=({name:t,fields:s,label:p,optional:j,...y})=>{const r=d.useRef(!1),{control:x,setValue:o,formState:{errors:b}}=I(),f=w({name:t,control:x}),c=d.useCallback(()=>{const v=s.reduce((m,n)=>({...m,[n.name]:n.defaultValue}),{});r.current===!1?o(t,[v]):r.current=!1},[s,t,o]),u=d.useCallback(()=>{o(t,[]),r.current=!0},[t,o]),i=E(t,b),h=p?e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(A,{children:p}),j&&e.jsx(B,{gray:!0,className:"ml-1.5",children:"optional"})]}):null;return!f||L(f)?e.jsxs("div",{className:"flex flex-col gap-2",children:[h,i.info&&e.jsx(C,{intent:i.intent,children:i.info}),e.jsx(g,{type:"button",variant:"outline",onClick:c,"aria-label":"Add","data-test":`${t}-0-plus-button`,children:e.jsx(V,{className:"w-4 h-4"})},"append-not-empty")]}):e.jsxs("div",{className:"flex flex-col gap-2",children:[h,e.jsx(q,{name:t,fields:s,...y,onClear:u})]})},M=d.memo(z);export{M as A};
