let agents,loadingPromise;import{rankWith as e,and as t,mapStateToControlProps as r,mapDispatchToControlProps as o,uiTypeIs as n,formatIs as s}from"@jsonforms/core";import i,{OREvent as a}from"@openremote/core";import{AssetModelUtil as d}from"@openremote/model";import{getTemplateWrapper as m}from"@openremote/or-json-forms";import{InputType as p}from"@openremote/or-mwc-components/or-mwc-input";import{html as l}from"lit";import"@openremote/or-mwc-components/or-mwc-input";import{i18next as c}from"@openremote/or-translate";import{until as g}from"lit/directives/until.js";let subscribed=!1,timeout=2e3;function loadAgents(){return agents?Promise.resolve(agents):loadingPromise||(subscribed||(i.addListener(e=>{e===a.DISPLAY_REALM_CHANGED&&(agents=void 0,loadingPromise=void 0)}),i.events.subscribeAssetEvents(void 0,!1,e=>{e.asset&&e.asset.type.endsWith("Agent")&&(agents=void 0,loadingPromise=void 0)}),subscribed=!0),loadingPromise=i.rest.api.AssetResource.queryAssets({realm:{name:i.displayRealm},types:["Agent"],select:{attributes:[]}}).then(e=>e.data).then(e=>(agents=e,e)))}let agentIdTester=e(6,t(n("Control"),s("or-agent-id"))),agentIdRenderer=(e,t)=>{t=Object.assign(Object.assign(Object.assign({},t),r({jsonforms:Object.assign({},e)},t)),o(e.dispatch));let n=loadAgents().then(e=>{let r=e.map(e=>[e.id,e.name+" ("+e.id+")"]);return l`
            <or-mwc-input .label="${c.t("agentId")}" required class="agent-id-picker" @or-mwc-input-changed="${r=>(e=>{if(agents&&e){let r=d.getAssetDescriptor(e.type);r&&t.handleChange("",{id:e.id,type:r.agentLinkType})}})(e.find(e=>e.id===r.detail.value))}" type="${p.SELECT}" .value="${t.data}" .placeholder="${c.t("selectAgent")}" .options="${r}"></or-mwc-input>
        `});return m(l`
        <style>
            .agent-id-picker {
                min-width: 300px;
                max-width: 600px;
                width: 100%;
            }
        </style>
        ${g(n,l`<or-mwc-input class="agent-id-picker" .type="${p.SELECT}"></or-mwc-input>`)}
        `,void 0)};export const agentIdRendererRegistryEntry={tester:agentIdTester,renderer:agentIdRenderer};