"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=!0,exports.defineWebglEvent=defineWebglEvent;var _tool=require("./tool"),_constant=require("../common/constant"),_eventEmitter=require("../common/event-emitter"),_throttle=_interopRequireDefault(require("lodash/throttle"));let clickTimer;function defineWebglEvent(a,b){return c=>d=>{function e(a,b,c){let f;if(b){const g=d.filter(a=>!!a.model&&a.model.target.uuid===b.uuid&&(!a.model.isInstancedMesh||a.model.instancedId===c),!1);return g[0]?(f=g[0],f):(f=e(a,b.parent,c),f)?f:f}}let f=!1;const g=d.container;if(!g)throw Error("el \u4E0D\u5B58\u5728");let h;-1<[_constant.ModelMouseEvent.Click,_constant.ModelMouseEvent.ContextMenu].indexOf(a)&&(h=function(a){let b=[0,0];const c=a=>{b=[a.x,a.y]},d=a=>{(2<Math.abs(a.x-b[0])||2<Math.abs(a.y-b[1]))&&setTimeout(()=>{clearTimeout(clickTimer),clickTimer=null},0)};return a.addEventListener("mousedown",c),a.addEventListener("mouseup",d),()=>{a.removeEventListener("mousedown",c),a.removeEventListener("mouseup",d)}}(g));let i=0,j=h=>{if(a===_constant.ModelMouseEvent.Click||a===_constant.ModelMouseEvent.ContextMenu)if(!clickTimer)return i=new Date().getTime(),void(clickTimer=setTimeout(()=>{j.call(h,h)},200));else{const a=new Date().getTime();if(clearTimeout(clickTimer),clickTimer=null,200>a-i)return}if(a===_constant.ModelMouseEvent.DBlclick&&clickTimer&&(clearTimeout(clickTimer),clickTimer=null),!f){const f=h,i=d.component?[d.component]:[],j=(0,_tool.getIntersects)(f,g,d.camera,d.scene.children).filter(a=>a.object.isMesh||a.object.isSprite),k=j[0]||{},l={event:f,object:k.object,uv:k.uv,point:k.point,component:void 0,distance:k.distance,instanceId:k.instanceId};if(k.object&&(k.object.isMesh||k.object.isSprite)){const a=k.object;l.component=e(i,a,l.instanceId)}const m=new _eventEmitter.DTEvent(a,l);b.call(null,d,m,c)}};g.addEventListener(a,(0,_throttle.default)(j,80));return{name:a,type:"event",disable:()=>{f=!0},enable:()=>{f=!1},remove:()=>{g.removeEventListener(a,j),h&&h()}}}}