import{and as e,createCombinatorRenderInfos as r,findUISchema as t,hasType as o,isAllOfControl as n,isAnyOfControl as a,isBooleanControl as s,isDateControl as i,isDateTimeControl as l,isEnumControl as c,isIntegerControl as m,isNumberControl as p,isObjectControl as d,isOneOfControl as u,isOneOfEnumControl as f,isStringControl as O,isTimeControl as y,mapDispatchToControlProps as h,mapStateToControlProps as b,mapStateToControlWithDetailProps as j,mapStateToLayoutProps as g,or as x,rankWith as C,resolveSchema as v,schemaMatches as q,schemaSubPathMatches as T,uiTypeIs as R}from"@jsonforms/core";import{html as A}from"lit";import"@openremote/or-mwc-components/or-mwc-input";import"@openremote/or-vaadin-components/or-vaadin-input";import{getCombinatorInfos as S,getLabel as $,getSchemaConst as F,getSchemaPicker as L,getTemplateFromProps as w,mapStateToCombinatorRendererProps as I}from"./util";import"./layouts/layout-vertical-element";import"./controls/control-input-element";import"./controls/control-array-element";import{Util as V}from"@openremote/core";let hasOneOfItems=e=>void 0!==e.oneOf&&e.oneOf.length>0&&e.oneOf.every(e=>void 0!==F(e)),hasEnumItems=e=>Array.isArray(e.enum);export const isEnumArray=e(R("Control"),e(q(e=>o(e,"array")&&!Array.isArray(e.items)&&!0===e.uniqueItems),T("items",e=>hasOneOfItems(e)||Array.isArray(e.enum))));export const verticalOrGroupLayoutTester=C(1,x(R("VerticalLayout"),R("Group")));export const verticalLayoutRenderer=(e,r)=>{let t,o=Object.assign(Object.assign(Object.assign({},g({jsonforms:Object.assign({},e)},r)),h(e.dispatch)),{label:r.label,required:r.required,errors:r.errors,minimal:r.minimal,type:r.type}),n=A`<or-json-forms-vertical-layout style="max-width: 100%" .state="${e}" .props="${o}"></or-json-forms-vertical-layout>`;return!o.required&&o.path&&(t=()=>{o.handleChange(o.path||"",void 0)}),getTemplateWrapper(n,t)};export const constTester=C(6,q(e=>void 0!==F(e)));export const constRenderer=(e,r)=>{};export const inputControlTester=C(3,x(q(e=>Array.isArray(e.type)&&7===e.type.length),O,s,p,m,i,y,l,c,f,isEnumArray));export const inputControlRenderer=(e,r)=>{let t,o=Object.assign(Object.assign({},b({jsonforms:Object.assign({},e)},r)),h(e.dispatch));o.label=r.label||o.label,o.required=!!r.required||o.required;let n=A`<or-json-forms-input-control .state="${e}" .props="${o}"></or-json-forms-input-control>`;return!o.required&&o.path&&(t=()=>{o.handleChange(o.path,void 0)}),getTemplateWrapper(n,t)};export const objectControlTester=C(2,d);export const objectControlRenderer=(e,r)=>{let{required:o,renderers:n,cells:a,uischemas:s,schema:i,label:l,errors:c,path:m,visible:p,enabled:d,uischema:u,rootSchema:f}=j({jsonforms:Object.assign({},e)},r),O=t(s,i,u.scope,m,"VerticalLayout",u,f);return w(e,{visible:p,enabled:d,schema:i,uischema:O,path:m,renderers:n,cells:a,label:r.label||$(i,f,l)||"",required:!!r.required||!!o,errors:c,minimal:r.minimal})};export const anyOfOneOfControlTester=C(4,x(a,u));export const anyOfOneOfControlRenderer=(e,t)=>{let o={jsonforms:Object.assign({},e)},{required:n,renderers:a,cells:s,schema:i,label:l,path:c,errors:m,visible:p,enabled:d,uischema:u,rootSchema:f,data:O}=j(o,t),y=void 0!==i.anyOf?"anyOf":"oneOf",b=v(i,y,f),g=I(o,t,y),x=r(b,f,y,g.uischema||u,c,g.uischemas);if(null!=O&&(void 0===g.indexOfFittingSchema||g.indexOfFittingSchema<0)){let e=S(b,f),r=e.length>0?e[0].constProperty:void 0;if(r&&"object"==typeof O&&O[r]){let t=O[r];g.indexOfFittingSchema=e.findIndex(e=>e.constValue===t)}}if(void 0===g.indexOfFittingSchema||g.indexOfFittingSchema<0){let{handleChange:r}=h(e.dispatch);return L(f,b,t.label||l,e=>r(c,e.defaultValueCreator()))}let C=x[g.indexOfFittingSchema].schema,q=x[g.indexOfFittingSchema].uischema;return C.allOf&&(q={type:"Control",scope:"#",label:!1}),w(e,{schema:C,uischema:q,path:c,renderers:a,cells:s,label:t.label||$(C,f,l)||"",required:t.required||!!n,errors:m,minimal:t.minimal,type:C.title})};export const allOfControlTester=C(4,n);export const allOfControlRenderer=(e,r)=>{let t=Object.assign({},j({jsonforms:Object.assign({},e)},r)),o=v(t.schema,"allOf",t.rootSchema);return t.schema=o.allOf.reduce((e,r)=>V.mergeObjects(e,r,!1)),t.uischema.scope="#",t.label=r.label||t.label,t.required=!!r.required||t.required,t.minimal=r.minimal,w(e,t)};export const arrayControlTester=C(2,q(e=>o(e,"array")&&!Array.isArray(e.items)));export const arrayControlRenderer=(e,r)=>{let t,o=Object.assign(Object.assign({},b({jsonforms:Object.assign({},e)},r)),h(e.dispatch));o.label=r.label||o.label,o.required=!!r.required||o.required,o.minimal=r.minimal;let n=A`<or-json-forms-array-control style="max-width: 100%" .state="${e}" .props="${o}"></or-json-forms-array-control>`;return!o.required&&o.path&&(t=()=>{o.handleChange(o.path,void 0)}),getTemplateWrapper(n,t)};export function getTemplateWrapper(e,r){let t=r?A`
                <button class="button-clear" @click="${r}"><or-icon icon="close-circle"></or-icon></input>
            `:"";return A`
                <div class="item-container">
                    ${e}
                    <div class="delete-container">
                        ${t}
                    </div>
                </div>
            `}export function unknownTemplate(){return A`<span>No applicable renderer found!</span>`}export const StandardRenderers=[{tester:verticalOrGroupLayoutTester,renderer:verticalLayoutRenderer},{tester:constTester,renderer:constRenderer},{tester:inputControlTester,renderer:inputControlRenderer},{tester:objectControlTester,renderer:objectControlRenderer},{tester:arrayControlTester,renderer:arrayControlRenderer},{tester:anyOfOneOfControlTester,renderer:anyOfOneOfControlRenderer},{tester:allOfControlTester,renderer:allOfControlRenderer}];