import{definePlugin}from"../../core/extension";import{Box3,Vector3}from"three";import throttle from"lodash/throttle";export default definePlugin("lod",{setup(a={},b){let{include:d="",exclude:e=["floor","room","wall","group","city","campus","building"],size:f=9,distance:g=1/0,throttle:h=50,control:c}=a,i=!0;const j=new Box3,k=a=>{if(j.setFromObject(a),!j.max.x||!j.min.x)return{modelSize:0,modelDistance:0};const c=[new Vector3(j.min.x,j.min.y,j.min.z),new Vector3(j.max.x,j.min.y,j.min.z)];let d=1/0,e=-Infinity,f=1/0,g=-Infinity;c.forEach(a=>{const c=a.clone().project(b.camera),h=(.5*c.x+.5)*b.container.clientWidth,i=(.5*-c.y+.5)*b.container.clientHeight;d=Math.min(d,h),e=Math.max(e,h),f=Math.min(f,i),g=Math.max(g,i)});const h=e-d,i=g-f,k=Math.max(h,i),l=new Vector3;j.getCenter(l);let m=b.camera.position.distanceTo(l);return{modelSize:k,modelDistance:m}},l=(a,b,c=!0)=>{let d=a.ext.type.toLowerCase();if(b&&"string"==typeof b)return d===b.toLowerCase();if(Array.isArray(b)){let a=b.map(a=>a.toLowerCase());return-1<a.indexOf(d)}return"function"==typeof b?b(a):c},m=a=>{b.component&&(!a&&(a=b.component,a.model.updateMatrixWorld(!0)),"__lod_visible__"in a.ext&&(a.model.visible=a.ext.__lod_visible__),a.filter(a=>(m(a),!1),!0))},n=a=>{if(!i||!b.component)return;a||(a=b.component,a.model.updateMatrixWorld(!0)),a.ext.__lod_visible__||(a.ext.__lod_visible__=a.model.visible);let{modelSize:c,modelDistance:h}=k(a.model.target),j=l(a,e,!1);if(!j){if(l(a,d)&&(c<f||h>g))return void(a.model.visible=!1);a.model.visible=!0}a.filter(a=>(n(a),!1),!0)};if(!c){let a=b.plugin.get("orbit-control");if(!a)throw"\u9700\u8981orbit-control\u5B9E\u4F8B";c=a.control}let o=throttle(n,h);return o(),c.addEventListener("start",()=>{o()}),{reset:m,enable:()=>{i=!0},disable:()=>{i=!1},update:n}},render(){},destroyed(){this.reset(),this.disable()}});