import{composeWithUi as e,createDefaultValue as t,deriveTypes as r,getAjv as o,getData as i,getRenderers as a,getSchema as n,hasShowRule as s,isVisible as l,Resolve as c}from"@jsonforms/core";import{DefaultColor5 as m,Util as p}from"@openremote/core";import{i18next as d}from"@openremote/or-translate";import"@openremote/or-components/or-ace-editor";import{html as u,unsafeCSS as f}from"lit";import{createRef as v,ref as h}from"lit/directives/ref.js";import{unknownTemplate as b}from"./standard-renderers";import{OrMwcDialog as g,showDialog as S}from"@openremote/or-mwc-components/or-mwc-dialog";import"@openremote/or-vaadin-components/or-vaadin-select";import"@openremote/or-vaadin-components/or-vaadin-button";export function getTemplateFromProps(e,t){let r;if(!e||!t)return u``;let o=t.renderers||a({jsonforms:Object.assign({},e)}),i=t.schema,n=t.uischema;if(o&&i&&n&&e.core){let a=o.map(t=>[t,t.tester(n,i,{rootSchema:resolveSubSchemasRecursive(i,e.core.schema),config:e.config})]).sort((e,t)=>t[1]-e[1]),s=a&&a.length>0?a[0]:void 0;r=s&&-1!==s[1]?s[0].renderer(e,t):b()}return r}export function getCombinatorInfos(e,o){return e.map(e=>{let i,a,n,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],a=getSchemaConst(r[1]),n=()=>{let e={};return e[r[0]]=getSchemaConst(r[1]),e},s[0]||(s[0]=getSchemaConst(r[1]))):n=()=>t(e,o)}else n=()=>t(e,o);return{title:s[0],description:s[1],defaultValueCreator:n,constProperty:i,constValue:a}})}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){let i=getCombinatorInfos(t,e),a=i.map((e,t)=>({value:t+"",label:e.title||d.t("schema.title.indexedItem",{index:t})})),n=r?d.t("schema.anyOfPickerLabel",{label:r}):d.t("type");return u`
        <or-vaadin-select class="any-of-picker" .items=${a} @change=${e=>{o(i[Number(e.currentTarget.value)])}}>
            <span slot="label">${n}</span>
        </or-vaadin-select>
    `}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[d.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,a,n;if("object"!=typeof e||null===e)return -1;let s=[];for(let e=0;e<r[t].length;e++){let i=r[t][e];i.$ref&&(i=c.schema(o,i.$ref,o)),s.push(i)}let l=-1;for(let t=0;t<s.length;t++){let o=s[t],c=(null==(i=null==r?void 0:r.discriminator)?void 0:i.propertyName)||"type",m=null==(n=null==(a=o.properties)?void 0:a[c])?void 0:n.const;if(void 0!==m&&e[c]===m){l=t;break}}return l}export function mapStateToCombinatorRendererProps(t,r,a){let{uischema:m}=r,p=e(m,r.path),d=n(t),u=c.schema(r.schema||d,m.scope,d),f=void 0===r.visible||s(m)?l(m,i(t),r.path,o(t)):r.visible,v=r.id,h=c.data(i(t),p),b=u||d,g=getCombinatorIndexOfFittingSchema(h,a,b,d);return{data:h,path:p,schema:b,rootSchema:d,visible:f,id:v,indexOfFittingSchema:g,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=v(),i=v();S(new g().setContent(u`
                <or-ace-editor ${h(o)} @or-ace-editor-edit="${()=>void(i.value.disabled=!0)}" @or-ace-editor-changed="${e=>{let t;return t=e.detail.valid,void(i.value.disabled=!t)}}" .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:u`
                        <or-vaadin-button ${h(i)} disabled>
                            <or-translate value="update"></or-translate>
                        </or-vaadin-button>
                    `}]).setHeading(e).setDismissAction(null).setStyles(u`
                <style>
                    .mdc-dialog__surface {
                        width: 1024px;
                        overflow-x: visible !important;
                        overflow-y: visible !important;
                    }
                    #dialog-content {
                        border-color: var(--or-app-color5, ${f(m)});
                        border-top-width: 1px;
                        border-top-style: solid;
                        border-bottom-width: 1px;
                        border-bottom-style: solid;
                        padding: 0;
                        overflow: visible;
                        height: 60vh;
                    }
                </style>
            `))};