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 b,mapStateToControlProps as y,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 $}from"lit";import"@openremote/or-mwc-components/or-mwc-input";import{getCombinatorInfos as A,getLabel as S,getSchemaConst as w,getSchemaPicker as F,getTemplateFromProps as I,mapStateToCombinatorRendererProps as L,showJsonEditor as E}from"./util";import"./layouts/layout-vertical-element";import"./controls/control-input-element";import"./controls/control-array-element";import{InputType as V}from"@openremote/or-mwc-components/or-mwc-input";import{Util as W}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)||hasEnumItems(e))));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)),b(e.dispatch)),{label:r.label,required:r.required,errors:r.errors,minimal:r.minimal,type:r.type}),n=$`<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({},y({jsonforms:Object.assign({},e)},r)),b(e.dispatch));o.label=r.label||o.label,o.required=!!r.required||o.required;let n=$`<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||S(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",y=x(i,h,f),j=L(o,t,h),v=r(y,f,h,j.uischema||u,c,j.uischemas);if(null!=O&&(void 0===j.indexOfFittingSchema||j.indexOfFittingSchema<0)){let e=A(y,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}=b(e.dispatch);return null==O?F(f,y,c,h,t.label||l,e=>r(c,e.defaultValueCreator())):(console.warn("Cannot match "+h+" schema to instance data"),$`
                <div class="item-container no-match-container"><span>${l}:</span><b><or-translate value="validation.noSchemaMatchFound"></b><or-mwc-input .type="${V.BUTTON}" outlined label="json" icon="pencil" @or-mwc-input-changed="${e=>{e.stopPropagation(),E(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||S(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)=>W.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({},y({jsonforms:Object.assign({},e)},r)),b(e.dispatch));o.label=r.label||o.label,o.required=!!r.required||o.required,o.minimal=r.minimal;let n=$`<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?$`
                <button class="button-clear" @click="${r}"><or-icon icon="close-circle"></or-icon></input>
            `:"";return $`
                <div class="item-container">
                    ${e}
                    <div class="delete-container">
                        ${t}
                    </div>
                </div>
            `}export function unknownTemplate(){return $`<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}];