import{definePlugin}from"../../core/extension";import Tacos,{EventEmitter,Root,SocketResponseType}from"@tslfe/tacos-sdk";export let EventType=function(a){return a.device="device",a.space="space",a.spaceEvent="spaceEvent",a.scene="scene",a["*"]="*",a}({});export default definePlugin("os-animation",{setup(a,b){let c,d=new EventEmitter;const e=async()=>{var a;let b=await(null===(a=c)||void 0===a?void 0:a.createInstance(Root));null===b||void 0===b?void 0:b.addListener("*",a=>{let{data:c=[],messageType:b}=a;c.forEach(a=>{let e;e=b===SocketResponseType.deviceProperty?EventType.device:b===SocketResponseType.spaceProperty?EventType.space:b===SocketResponseType.spaceEvent?EventType.spaceEvent:b,d.emit(e,c),d.emit("*",c)})})},f=async a=>{let b=await c.createInstance(Root);return b.search(a).then(b=>{const c=[];if(b.map(a=>{a.layouts.map(a=>{"glb"===a.data.type&&a.data.url&&c.push(a.data.url)})}),0===c.length)throw Error(`${a}路径不存在`);else if(1<c.length);let d=c[0];return d})},g=async(a,c)=>/^\//.test(a)?f(a).then(a=>("function"==typeof c&&(a=c(a)),b.render(a,!0))):("function"==typeof c&&(a=c(a)),b.render(a,!0));return{loadModelUrl:f,enter:(a,b)=>g(a,b),compile:(a,c)=>f(a).then(a=>("function"==typeof c&&(a=c(a)),b.compile(a))),event:d,connect:async a=>Tacos.connect(a).then(async a=>(c=a,await e(),a)),dispose:()=>{c&&c.destroy(),d.removeAllListener()}}},destroyed(){this.dispose()}});