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