let agents,loadingPromise;import e,{OREvent as t}from"@openremote/core";import{AssetModelUtil as r}from"@openremote/model";import{getTemplateWrapper as s,rankWith as n,and as i,mapStateToControlProps as o,mapDispatchToControlProps as a,uiTypeIs as d,formatIs 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{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=n(6,i(d("Control"),m("or-agent-id"))),agentIdRenderer=(e,t)=>{t=Object.assign(Object.assign(Object.assign({},t),o({jsonforms:Object.assign({},e)},t)),a(e.dispatch));let n=loadAgents().then(e=>{let s=e.map(e=>[e.id,e.name+" ("+e.id+")"]);return l`
            <or-mwc-input .label="${g.t("agentId")}" required class="agent-id-picker" @or-mwc-input-changed="${s=>(e=>{if(agents&&e){let s=r.getAssetDescriptor(e.type);s&&t.handleChange("",{id:e.id,type:s.agentLinkType})}})(e.find(e=>e.id===s.detail.value))}" type="${p.SELECT}" .value="${t.data}" .placeholder="${g.t("selectAgent")}" .options="${s}"></or-mwc-input>
        `});return s(l`
        <style>
            .agent-id-picker {
                min-width: 300px;
                max-width: 600px;
                width: 100%;
            }
        </style>
        ${c(n,l`<or-mwc-input class="agent-id-picker" .type="${p.SELECT}"></or-mwc-input>`)}
        `,void 0)};export const agentIdRendererRegistryEntry={tester:agentIdTester,renderer:agentIdRenderer};