import{composeWithUi as e,createDefaultValue as t,deriveTypes as r,getAjv as o,getData as i,getRenderers as n,getSchema as a,hasShowRule as s,isVisible as l,Resolve as c}from"@jsonforms/core";import{DefaultColor5 as m,Util as p}from"@openremote/core";import{InputType as f}from"@openremote/or-mwc-components/or-mwc-input";import{i18next as u}from"@openremote/or-translate";import"@openremote/or-components/or-ace-editor";import{html as d,unsafeCSS as h}from"lit";import{createRef as b,ref as v}from"lit/directives/ref.js";import{unknownTemplate as O}from"./standard-renderers";import{OrMwcDialog as S,showDialog as g}from"@openremote/or-mwc-components/or-mwc-dialog";export function getTemplateFromProps(e,t){let r;if(!e||!t)return d``;let o=t.renderers||n({jsonforms:Object.assign({},e)}),i=t.schema,a=t.uischema;if(o&&i&&a&&e.core){let n=o.map(t=>[t,t.tester(a,i,{rootSchema:resolveSubSchemasRecursive(i,e.core.schema),config:e.config})]).sort((e,t)=>t[1]-e[1]),s=n&&n.length>0?n[0]:void 0;r=s&&-1!==s[1]?s[0].renderer(e,t):O()}return r}export function getCombinatorInfos(e,o){return e.map(e=>{let i,n,a,s=findSchemaTitleAndDescription(e,o);if(e.$ref&&(e=c.schema(e,"",o)),Array.isArray(e.allOf)&&(e=c.schema(e,"allOf",o)),r(e).every(e=>"object"===e)){let r=getSchemaObjectProperties(e).find(([e,t])=>void 0!==getSchemaConst(t));r?(i=r[0],n=getSchemaConst(r[1]),a=()=>{let e={};return e[r[0]]=getSchemaConst(r[1]),e},s[0]||(s[0]=getSchemaConst(r[1]))):a=()=>t(e,o)}else a=()=>t(e,o);return{title:s[0],description:s[1],defaultValueCreator:a,constProperty:i,constValue:n}})}export function getSchemaConst(e){if(e){if(void 0!==e.const)return e.const;if(Array.isArray(e.enum)&&1===e.enum.length)return e.enum[0]}}export function getSchemaPicker(e,t,r,o,i,n){let a=getCombinatorInfos(t[o],e),s=a.map((e,t)=>[t+"",e.title||u.t("schema.title.indexedItem",{index:t})]),l=i?u.t("schema.anyOfPickerLabel",{label:i}):u.t("type");return d`                
        <or-mwc-input class="any-of-picker" .label="${l}" .type="${f.SELECT}" .options="${s}" @or-mwc-input-changed="${e=>{n(a[Number(e.detail.value)])}}"></or-mwc-input>
    `}export function findSchemaTitleAndDescription(e,t){let r;if(e.$ref&&(r=getLabelFromScopeOrRef(e.$ref),e=c.schema(e,"",t)),e.title)return[e.title,e.description];if(e.allOf){let r=c.schema(e,"allOf",t).allOf.find(e=>!!e.title);if(r)return[r.title,r.description]}return[u.t("schema.title."+r,{defaultValue:p.camelCaseToSentenceCase(r)}),void 0]}function getLabelFromScopeOrRef(e){return e.substr(e.lastIndexOf("/")+1)}function getSchemaObjectProperties(e){let t=[];return e.allOf?t=e.allOf.map(e=>e.properties?Object.entries(e.properties):[]).flat():e.properties&&(t=Object.entries(e.properties)),t}let COMBINATOR_IDENTIFICATION_PROPERTY="type";export function getCombinatorIndexOfFittingSchema(e,t,r,o){var i,n;if("object"!=typeof e||null===e)return -1;let a=[];for(let e=0;e<r[t].length;e++){let i=r[t][e];i.$ref&&(i=c.schema(o,i.$ref,o)),a.push(i)}let s=-1;for(let t=0;t<a.length;t++){let r=null==(n=null==(i=a[t].properties)?void 0:i[COMBINATOR_IDENTIFICATION_PROPERTY])?void 0:n.const;if(void 0!==r&&e[COMBINATOR_IDENTIFICATION_PROPERTY]===r){s=t;break}}return s}export function mapStateToCombinatorRendererProps(t,r,n){let{uischema:m}=r,p=e(m,r.path),f=a(t),u=c.schema(r.schema||f,m.scope,f),d=void 0===r.visible||s(m)?l(m,i(t),r.path,o(t)):r.visible,h=r.id,b=c.data(i(t),p),v=u||f,O=getCombinatorIndexOfFittingSchema(b,n,v,f);return{data:b,path:p,schema:v,rootSchema:f,visible:d,id:h,indexOfFittingSchema:-1!==O?O:0,uischemas:t.jsonforms.uischemas,uischema:m}}export function getLabel(e,t,r,o){if(r)return r;let i=findSchemaTitleAndDescription(e,t);return i[0]?i[0]:o?p.camelCaseToSentenceCase(getLabelFromScopeOrRef(o)):void 0}export function resolveSubSchemasRecursive(e,t,r){return e.$ref?resolveSubSchemasRecursive(c.schema(t,e.$ref,t),t):((r?[r]:["allOf","anyOf","oneOf"]).forEach(r=>{let o=e[r];o&&(e[r]=o.map(e=>resolveSubSchemasRecursive(e,t)))}),e.items&&(Array.isArray(e.items)?e.items=e.items.map(e=>resolveSubSchemasRecursive(e,t)):e.items=resolveSubSchemasRecursive(e.items,t)),e.properties&&Object.keys(e.properties).forEach(r=>e.properties[r]=resolveSubSchemasRecursive(e.properties[r],t)),e)}export const controlWithoutLabel=e=>({type:"Control",scope:e,label:!1});export const showJsonEditor=(e,t,r)=>{let o=b(),i=b();g(new S().setContent(d`
                <or-ace-editor ${v(o)} @or-ace-editor-edit="${()=>void(i.value.disabled=!0)}" @or-ace-editor-changed="${e=>(e=>{let t=e.detail.valid;i.value.disabled=!t})(e)}" .value="${t}"></or-ace-editor>
            `).setActions([{actionName:"cancel",content:"cancel"},{default:!0,actionName:"update",action:()=>{let e=o.value;e.validate()&&r(e.getValue()?JSON.parse(e.getValue()):void 0)},content:d`<or-mwc-input ${v(i)} disabled .type="${f.BUTTON}" label="update"></or-mwc-input>`}]).setHeading(e).setDismissAction(null).setStyles(d`
                <style>
                    .mdc-dialog__surface {
                        width: 1024px;
                        overflow-x: visible !important;
                        overflow-y: visible !important;
                    }
                    #dialog-content {
                        border-color: var(--or-app-color5, ${h(m)});
                        border-top-width: 1px;
                        border-top-style: solid;
                        border-bottom-width: 1px;
                        border-bottom-style: solid;
                        padding: 0;
                        overflow: visible;
                        height: 60vh;
                    }
                </style>
            `))};