const AUTOFORM_BLUEPRINT=globalThis.Util.makeDom('\n
\n'),AUTOFORM_STYLE=globalThis.Util.makeDom(""),DB_FORM_TYPE_MAP={i:"number",ui:"number",bi:"number",ubi:"number",ti:"number",f:"number",ff:"number",b:"switch",d:"date",dt:"datetime",t:"textarea"},getDbFormType=t=>DB_FORM_TYPE_MAP[t]||"text",normalizeSchemaItem=t=>{if(!t?.tableID)return t;const e=t.settings||{};return{...t,name:t.name,label:e.label??t.label??t.name,type:e.type??getDbFormType(t.type),options:e.options??t.options,placeholder:e.placeholder??t.placeholder,setting:e.attrs??t.setting}};globalThis.Component.register("AutoForm",t=>{Array.isArray(t.state.schema)||(t.state.schema=[]);const e=e=>{t.state._schema=Array.isArray(e)?e.map(normalizeSchemaItem):[]};t.state.__watch("schema",e),e(t.state.schema);const a=e=>{const a=(i=e||{})&&"object"==typeof i&&!i.__isProxy?globalThis.NewState(i):i;var i;t.data=a,t.state.data!==a&&(t.state.data=a)};t.state.__watch("data",a),a(t.state.data),t.vertical=t.hasAttribute("vertical"),t.horizontal=t.hasAttribute("horizontal"),t.inline=t.hasAttribute("inline"),t.nobutton=t.hasAttribute("nobutton"),t.submitlabel=t.getAttribute("submitlabel")||"",t.request={method:"POST"},t.response={},t.result=null,t.form=globalThis.$(t,"form"),t.submit=(e={})=>{if(!t.form.reportValidity())return globalThis.Toast?.show("{#verify failed#}",{type:"danger"});if(!t.dispatchEvent(new CustomEvent("submit",{detail:t.data,cancelable:!0,bubbles:!1})))return;const a={...t.request,data:t.data,noui:!0,...e};let i=null;if(t.api)i=t.api.do(a);else{if(!t.request.url)return console.warn("{#please config .api or .request.url to auto submit#}");i=globalThis.HTTP.request(a)}i.then(e=>{if(t.response=e,t.result=e.result,"object"==typeof e.result&&e.result.error)throw new Error(e.result.error);t.dispatchEvent(new CustomEvent("response",{detail:e,bubbles:!1}))}).catch(e=>{globalThis.Toast?.show(e.message,{type:"danger"}),t.dispatchEvent(new CustomEvent("error",{detail:e,bubbles:!0}))})}},AUTOFORM_BLUEPRINT,AUTOFORM_STYLE);const findAnchorInBlueprint=t=>{let e=t.querySelector("[control-wrapper]");if(e)return e;for(const a of t.querySelectorAll("template"))if(e=findAnchorInBlueprint(a.content),e)return e;return null},AutoForm={customTypes:[],register:(t,e)=>{const a=e||t;AutoForm.customTypes.find(e=>e.name===t)||(AutoForm.customTypes.push({name:t,typeName:a}),AutoForm._addAutoFormComponent(t,a))},_addAutoFormComponent:(t,e)=>{const a=findAnchorInBlueprint(AUTOFORM_BLUEPRINT);if(a){const i=globalThis.Util.makeDom(`<${t} $name="item.name" $.="item.setting || {}" $bind="thisNode.closest('AutoForm').data[item.name]" class="w-100">${t}>`);a.appendChild(i)}}};globalThis.AutoForm=AutoForm;