import{ComponentType}from"../common/constant";import EventEmitter from"../common/event-emitter";import{extend}from"../common/util";export function count(a){let b=0;return a.forEach(a=>{a.children&&(b+=count(a.children)),b++}),b}export const search=(a,b,c)=>filter(a,a=>a.type===b,c);export const get=(a,b,c)=>{let d=`${b}`,e=filter(a,a=>{if(a.modelId===d||a.id===d)return!0;let b=d.split(",");if(a.id&&0<b.length){let c=a.id.split(",");for(let a=0;a<b.length;a++)if(-1===c.indexOf(b[a]))return!1;return!0}return!1},c);return e[0]};export const filter=(a,b,c)=>{let d=[];return a.children.forEach(a=>{b(a)&&d.push(a),!c&&0<a.children.length&&(d=d.concat(a.filter(b,c)))}),d};export const add=async(a,b,c=!1)=>{c?await a.model.attach(b.model):await a.model.add(b.model),a.children.push(b),b.parent=a};export const replace=(a,b)=>{if(!a.parent)throw Error("top component can not be replace, please use meta.clear()");let c=a.parent,d=c.children.indexOf(a);-1<d&&(c.children.splice(d,1,b),a.model.replace(b.model),a.parent=void 0,b.parent=c)};export const remove=async(a,b)=>{await a.model.remove(b.model);let c=a.children.indexOf(b);-1<c&&a.children.splice(c,1),b.parent=void 0};export const removeFromParent=async a=>{if(await a.model.removeFromParent(),!!a.parent){let b=a.parent.children.indexOf(a);-1<b&&a.parent.children.splice(b,1),a.parent=void 0}};export const dispose=async a=>{a.children.forEach(a=>{a.dispose()}),a.removeFromParent(),a.removeAllListener(),await a.model.dispose()};export const clear=async a=>{a.children.forEach(a=>{a.dispose()})};class Group extends Array{constructor(a,b){Array.isArray(a)||(a=[]),super(...a),this.model=void 0,this.isGroup=!0,this.model=b}async removeFromParent(){for(let a=0;a<this.length;a++)await this[a].removeFromParent()}async dispose(){for(let a=0;a<this.length;a++)await this[a].dispose()}async clear(){for(let a=0;a<this.length;a++)await this[a].clear()}}export default function createComponentCreator(a,b){const c=async(d,e)=>{var f,g,h,i;const j=a.get(d.modelId),k={isComponent:!0,id:d.realId||"",get modelId(){return d.modelId||j.modelId},type:d.realType,ext:{tags:(null===(f=d.props)||void 0===f?void 0:f.tags)||[],type:d.type,typeCode:(null===(g=d.props)||void 0===g?void 0:g.type)||"",name:d.name,other:(null===(h=d.extension)||void 0===h?void 0:h.other)||{},geo:(null===(i=d.extension)||void 0===i?void 0:i.geo)||{}},children:[],get parent(){return e},set parent(a){e=a},get model(){return j}},l=new EventEmitter,m=extend({...k,...{search:(a,c=!1)=>{let d=search(m,a,c);return new Group(d,b(d.map(a=>a.model)))},filter:(a,c=!1)=>{let d=filter(m,a,c);return new Group(d,b(d.map(a=>a.model)))},get:(a,b=!1)=>get(m,a,b),add:async(a,b=!1)=>{if("string"==typeof a)a={url:a,type:ComponentType.unknow,realType:"",modelId:"",name:""};else if("isComponent"in a)return await add(m,a,b),m;if(!a.url)throw Error(`url 不可为空`);return await c(a,m)},replace:a=>{replace(m,a)},remove:a=>remove(m,a),removeFromParent:()=>removeFromParent(m),dispose:()=>dispose(m),clear:()=>clear(m),emit:a=>{l.emit(a),e&&!a.stopped&&e.emit(a)}}},l);return(d.children||[]).forEach(async a=>{let b=await c(a,m);m.children.push(b)}),m};return c}