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 h,mapDispatchToControlProps as y,mapStateToControlProps as b,mapStateToControlWithDetailProps as g,mapStateToLayoutProps as j,or as v,rankWith as C,resolveSchema as x,schemaMatches as T,schemaSubPathMatches as q,uiTypeIs as R}from"@jsonforms/core";import{html as A}from"lit";import{InputType as $}from"@openremote/or-mwc-components/or-mwc-input";import{getCombinatorInfos as S,getLabel as F,getSchemaConst as w,getSchemaPicker as L,getTemplateFromProps as I,mapStateToCombinatorRendererProps as V,showJsonEditor as W}from"./util";import"./layouts/layout-vertical-element";import"./controls/control-input-element";import"./controls/control-array-element";import{Util as E}from"@openremote/core";let hasOneOfItems=e=>void 0!==e.oneOf&&e.oneOf.length>0&&e.oneOf.every(e=>void 0!==w(e)),hasEnumItems=e=>Array.isArray(e.enum);export const isEnumArray=e(R("Control"),e(T(e=>o(e,"array")&&!Array.isArray(e.items)&&!0===e.uniqueItems),q("items",e=>hasOneOfItems(e)||Array.isArray(e.enum))));export const verticalOrGroupLayoutTester=C(1,v(R("VerticalLayout"),R("Group")));export const verticalLayoutRenderer=(e,r)=>{let t,o=Object.assign(Object.assign(Object.assign({},j({jsonforms:Object.assign({},e)},r)),y(e.dispatch)),{label:r.label,required:r.required,errors:r.errors,minimal:r.minimal,type:r.type}),n=A`<or-json-forms-vertical-layout .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,T(e=>void 0!==w(e)));export const constRenderer=(e,r)=>{};export const inputControlTester=C(3,v(T(e=>Array.isArray(e.type)&&7===e.type.length),O,s,p,m,i,h,l,c,f,isEnumArray));export const inputControlRenderer=(e,r)=>{let t,o=Object.assign(Object.assign({},b({jsonforms:Object.assign({},e)},r)),y(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}=g({jsonforms:Object.assign({},e)},r),O=t(s,i,u.scope,m,"VerticalLayout",u,f);return I(e,{visible:p,enabled:d,schema:i,uischema:O,path:m,renderers:n,cells:a,label:r.label||F(i,f,l)||"",required:!!r.required||!!o,errors:c,minimal:r.minimal})};export const anyOfOneOfControlTester=C(4,v(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}=g(o,t),h=void 0!==i.anyOf?"anyOf":"oneOf",b=x(i,h,f),j=V(o,t,h),v=r(b,f,h,j.uischema||u,c,j.uischemas);if(null!=O&&(void 0===j.indexOfFittingSchema||j.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];j.indexOfFittingSchema=e.findIndex(e=>e.constValue===t)}}if(void 0===j.indexOfFittingSchema||j.indexOfFittingSchema<0){let{handleChange:r}=y(e.dispatch);return null==O?L(f,b,c,h,t.label||l,e=>r(c,e.defaultValueCreator())):(console.warn("Cannot match "+h+" schema to instance data"),A`
                <div class="item-container no-match-container"><span>${l}:</span><b><or-translate value="validation.noSchemaMatchFound"></b><or-mwc-input .type="${$.BUTTON}" outlined label="json" icon="pencil" @or-mwc-input-changed="${e=>{e.stopPropagation(),W(l,O,e=>{r(c||"",e)})}}"></or-mwc-input></div>
            `)}let C=v[j.indexOfFittingSchema].schema,T=v[j.indexOfFittingSchema].uischema;return C.allOf&&(T={type:"Control",scope:"#",label:!1}),I(e,{schema:C,uischema:T,path:c,renderers:a,cells:s,label:t.label||F(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({},g({jsonforms:Object.assign({},e)},r)),o=x(t.schema,"allOf",t.rootSchema);return t.schema=o.allOf.reduce((e,r)=>E.mergeObjects(e,r,!1)),t.uischema.scope="#",t.label=r.label||t.label,t.required=!!r.required||t.required,t.minimal=r.minimal,I(e,t)};export const arrayControlTester=C(2,T(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)),y(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 .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}];