"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=!0,exports.default=exports.BoxSelectionEventType=void 0;var _extension=require("../../core/extension"),_constant=require("../../common/constant"),_tool=_interopRequireDefault(require("./tool")),_path=_interopRequireDefault(require("./path")),_three=require("three"),_util=require("../../common/util"),_eventEmitter=_interopRequireDefault(require("../../common/event-emitter"));const defaultPathOptions={color:"red",closed:!0,radius:.02,radialSegments:20,texture:void 0,mode:"polygon"};let BoxSelectionEventType=exports.BoxSelectionEventType=function(a){return a.Start="start",a.End="end",a.PointChange="point-change",a}({});var _default=exports.default=(0,_extension.definePlugin)("box-selection",{setup(a={mode:"polygon"},b){let c={...defaultPathOptions,...a};const d=new _eventEmitter.default;let e=(0,_tool.default)(),f=(0,_path.default)();b.plugin.has(e.key)||b.plugin.use(e),b.plugin.has(f.key)||b.plugin.use(f);let g,h=b.plugin.get(e.key),i=b.plugin.get(f.key),j=[],k=!1;const l=a=>{a.stopPropagation(),a.preventDefault();let b=h.screenPointToThreeCoordinate(a.clientX,a.clientY);if(b){j.push({x:b.x,y:b.y,z:b.z});let a=Math.max(...j.map(a=>a.y));j.map(b=>{b.y=a}),d.emit(BoxSelectionEventType.PointChange),2===j.length&&"rect"===c.mode&&o()}},m=a=>{a.stopPropagation(),a.preventDefault(),g=h.screenPointToThreeCoordinate(a.clientX,a.clientY)},n=a=>{a.stopPropagation(),a.preventDefault(),g=null,o()},o=()=>{let a=b.container;a.removeEventListener(_constant.ModelMouseEvent.Click,l,!0),a.removeEventListener(_constant.ModelMouseEvent.Mousemove,m,!0),a.removeEventListener(_constant.ModelMouseEvent.ContextMenu,n,!0),g=null,k&&d.emit(BoxSelectionEventType.End),k=!1,r(!0)};let p;const q=()=>{p&&i.remove(p),0<j.length&&d.emit(BoxSelectionEventType.PointChange),j=[],g=null},r=(a=!1)=>{if(!k&&!0!==a)return;let b=[...j];if(g&&b.push({...g,y:g.y+1}),2>b.length)return void(p&&i.remove(p));if(p&&i.remove(p),"rect"===c.mode){let[a,c]=b;b=[a,{x:c.x,y:c.y,z:a.z},c,{x:a.x,y:a.y,z:c.z}]}p=i.create(b,c),(0,_util.toArray)(p.material).forEach(a=>a.depthTest=!1)},s=()=>{let a=[...j];if(g&&a.push({...g,y:g.y+1}),"polygon"===c.mode&&3>a.length||"rect"===c.mode&&2>a.length)return[];if("rect"===c.mode){let[b,c]=a;a=[b,{x:c.x,y:c.y,z:b.z},c,{x:b.x,y:b.y,z:c.z}]}let d=new _three.Box2().setFromPoints(a.map(a=>new _three.Vector2(a.x,a.z)));return b.filter(a=>{let{x:b,z:c}=a.model.position;return d.containsPoint(new _three.Vector2(b,c))})};return{get list(){return s()},start:()=>{if(!k){k=!0;let a=b.container;a.addEventListener(_constant.ModelMouseEvent.Click,l,!0),a.addEventListener(_constant.ModelMouseEvent.Mousemove,m,!0),a.addEventListener(_constant.ModelMouseEvent.ContextMenu,n,!0),d.emit(BoxSelectionEventType.Start)}},clear:q,end:o,undo:(a=1)=>{1>a||(j.splice(j.length-a,a),!k&&1===j.length&&(j=[]),r(!0),d.emit(BoxSelectionEventType.PointChange))},dispose:()=>{q(),o(),d.removeAllListener()},addEventListener:(a,b)=>d.addEventListener(a,b),_update:r}},render(){this._update()},destroyed(){this.dispose()}});