var i=(m,d,n)=>new Promise((l,r)=>{var f=t=>{try{s(n.next(t))}catch(c){r(c)}},p=t=>{try{s(n.throw(t))}catch(c){r(c)}},s=t=>t.done?l(t.value):Promise.resolve(t.value).then(f,p);s((n=n.apply(m,d)).next())});import{b6 as o,cS as O,bi as u,cR as R}from"./bootstrap-Bqxdt4iG.js";import{u as T}from"./useEntityForm-CMwp7TbN.js";import{u as E}from"./useRoleAccess-rNIDgdat.js";import{u as V}from"./warehouses-BnkLPhlE.js";import{_ as $}from"./warehouse-create-drawer.vue_vue_type_script_setup_true_lang-Bz6tbPE-.js";import{a1 as q,L as B,E as C,ap as H,aj as U,p as b,a9 as y}from"../jse/index-index-BUJ_4tQs.js";import{u as Z}from"./use-drawer-CLDmRz16.js";const g="__add_new__",oe=q({__name:"location-form",props:{location:{default:null},mode:{},defaultWarehouseId:{default:""}},emits:["cancel","submit"],setup(m,{expose:d,emit:n}){const l=m,r=n,f=V(),{isWarehouseScoped:p,primaryWarehouseId:s}=E(),[t,c]=Z({connectedComponent:$}),h=B([]),_={label:`+ ${o("page.locations.form.addNewWarehouse")}`,value:g},w=()=>i(null,null,function*(){try{const e=yield f.fetchOptions();h.value=e}catch(e){console.error("Failed to load warehouse options:",e),h.value=[]}}),I=C(()=>[_,...h.value]);w();const W=(e,a)=>i(null,null,function*(){e===g&&(yield v.setValues({warehouseId:void 0}),c.open())}),k=e=>i(null,null,function*(){yield w(),yield v.setValues({warehouseId:e.id})}),A={warehouseId:p?s:l.defaultWarehouseId||"",inputCode:"",rack:"",level:"",locationType:O.BULK},S=C(()=>[{component:"Select",componentProps:{"data-testid":"location-form-warehouse-select",id:"location-form-warehouse-select",options:I.value,onChange:W,placeholder:o("page.locations.form.warehousePlaceholder"),showSearch:!0,disabled:p,filterOption:(e,a)=>a.value===g||a.label.toLowerCase().includes(e.toLowerCase())},fieldName:"warehouseId",label:o("page.locations.form.warehouse"),rules:"selectRequired"},{component:"Input",componentProps:{"data-testid":"location-form-inputcode-input",id:"location-form-inputcode-input",placeholder:o("page.locations.form.inputCodePlaceholder"),maxlength:10,onInput:e=>{const a=e.target;a.value=a.value.toUpperCase().replaceAll(/[^A-Z0-9]/g,"")}},fieldName:"inputCode",label:o("page.locations.form.inputCode"),help:o("page.locations.form.inputCodeHelp"),rules:u().min(1,o("page.locations.form.inputCodeRequired")).max(10,o("page.locations.form.inputCodeMaxLength")).regex(/^[A-Z0-9]+$/,o("page.locations.form.inputCodeFormat"))},{component:"Input",componentProps:{"data-testid":"location-form-rack-input",placeholder:o("page.locations.form.rackPlaceholder"),maxlength:2,onInput:e=>{const a=e.target;a.value=a.value.replaceAll(/\D/g,"")}},fieldName:"rack",label:o("page.locations.form.rack"),help:o("page.locations.form.rackHelp"),rules:u().optional().refine(e=>!e||/^\d{1,2}$/.test(e),o("page.locations.form.rackFormat"))},{component:"Input",componentProps:{"data-testid":"location-form-level-input",placeholder:o("page.locations.form.levelPlaceholder"),maxlength:2,onInput:e=>{const a=e.target;a.value=a.value.replaceAll(/\D/g,"")}},fieldName:"level",label:o("page.locations.form.level"),help:o("page.locations.form.levelHelp"),dependencies:{triggerFields:["rack"],rules:e=>e.rack?u().optional().refine(a=>!a||/^\d{1,2}$/.test(a),o("page.locations.form.levelFormat")):u().optional().refine(a=>!a,o("page.locations.form.levelRequiresRack"))}},{component:"Select",componentProps:{"data-testid":"location-form-type-select",options:R.map(e=>({label:e.label,value:e.value})),placeholder:o("page.locations.form.locationTypePlaceholder")},fieldName:"locationType",label:o("page.locations.form.locationType"),rules:"selectRequired"}]),{Form:N,formApi:v,focus:P,isDirty:x,resetForm:F,submitForm:D,watchEntity:L}=T({emptyValues:A,firstInputId:"location-form-warehouse-select",schema:S,wrapperClass:"grid-cols-1"});return L(()=>l.location,()=>l.mode),d({cancel:()=>{r("cancel")},focus:P,isDirty:x,resetForm:F,submit:()=>i(null,null,function*(){const e=yield D();e&&r("submit",e)})}),(e,a)=>(U(),H("div",null,[b(y(N)),b(y(t),{onSuccess:k})]))}});export{oe as _};
