!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Selection={})}(this,(function(t){"use strict";const e="chart-timeline-grid-row-cell",i="chart-timeline-items-row-item";const s="config.plugin.Selection";function a(t){return Object.assign({enabled:!0,showOverlay:!0,isSelecting:!1,pointerState:"up",selectKey:"",multiKey:"shift",multipleSelection:!0,targetType:"",targetData:null,initialPosition:{x:0,y:0},currentPosition:{x:0,y:0},selectionAreaLocal:{x:0,y:0,width:0,height:0},selectionAreaGlobal:{x:0,y:0,width:0,height:0},selecting:{[i]:[],[e]:[]},selected:{[i]:[],[e]:[]},lastSelected:{[i]:[],[e]:[]},automaticallySelected:{[i]:[],[e]:[]},events:{onStart(){},onSelecting:t=>t,onEnd:t=>t},pointerEvents:{down:null,move:null,up:null}},t)}class l{constructor(t,e){this.onDestroy=[],this.started=!1,this.vido=t,this.state=t.state,this.api=t.api,this.merge=this.state.get("config.merge"),this.state.update(s,a(e)),this.data=a(e),this.slotClassName=this.api.getClass("chart-selection"),this.slotStyleMap=new t.StyleMap({display:"none"}),this.html=t.html,this.slot=this.slot.bind(this),this.destroy=this.destroy.bind(this),this.setSlot(),this.onCellCreate=this.onCellCreate.bind(this),this.apiGetSelection=this.apiGetSelection.bind(this),this.apiGetSelecting=this.apiGetSelecting.bind(this),this.apiGetSelected=this.apiGetSelected.bind(this),this.apiSetSelection=this.apiSetSelection.bind(this),this.apiSelectCells=this.apiSelectCells.bind(this),this.apiSelectItems=this.apiSelectItems.bind(this),this.api.plugins.selection={getSelection:this.apiGetSelection,getSelected:this.apiGetSelected,getSelecting:this.apiGetSelecting,setSelection:this.apiSetSelection,selectCells:this.apiSelectCells,selectItems:this.apiSelectItems},this.state.update("config.chart.grid.cell.onCreate",(t=>(t.includes(this.onCellCreate)||t.push(this.onCellCreate),t))),this.onDestroy.push(this.state.subscribe("config.plugin.TimelinePointer",(t=>{this.pointerData=t,this.onPointerData()}))),this.updateData(),this.onDestroy.push(this.state.subscribe(s,(t=>{this.data=t}))),this.updateSelectionClassName=this.updateSelectionClassName.bind(this),this.selectedAction=this.selectedAction.bind(this),this.state.update("config.actions.chart-timeline-grid-row-cell",(t=>(t.includes(this.selectedAction)||t.push(this.selectedAction),t))),this.state.update("config.actions.chart-timeline-items-row-item",(t=>(t.includes(this.selectedAction)||t.push(this.selectedAction),t))),this.onDestroy.push(this.state.subscribe("config.chart.items",(t=>{this.data.selected[i]=this.data.selected[i].filter((e=>!!t[e])),this.data.selecting[i]=this.data.selecting[i].filter((e=>!!t[e]))}),{ignore:["config.chart.items.*.selected","config.chart.items.*.selecting"]}))}setSlot(){this.state.update("config.slots.chart-timeline-items.outer",(t=>(t.includes(this.slot)||t.push(this.slot),t)))}destroy(){this.state.update("config.slots.chart-timeline-items.outer",(t=>t.filter((t=>t!==this.slot)))),this.state.update("config.actions.chart-timeline-grid-row-cell",(t=>t.filter((t=>t!==this.selectedAction)))),this.state.update("config.actions.chart-timeline-items-row-item",(t=>t.filter((t=>t!==this.selectedAction)))),this.state.update("config.chart.grid.cell.onCreate",(t=>t.filter((t=>t!==this.onCellCreate)))),this.onDestroy.forEach((t=>t()))}updateData(){this.state.update(s,Object.assign({},this.data)),this.vido.update()}apiSetSelection(t){this.data.selected=this.api.mergeDeep({},t);let e=this.state.multi();e=this.updateCells(e),e=this.updateItems(e),e.done(),this.updateData()}apiSelectCells(t){this.data.selected[e]=[...t];let i=this.state.multi();i=this.updateCells(i),i.done(),this.updateData()}apiSelectItems(t){this.data.selected[i]=[...t];let e=this.state.multi();e=this.updateItems(e),e.done(),this.updateData()}apiGetSelection(){return{selecting:this.getSelectionWithData(this.data.selecting),selected:this.getSelectionWithData(this.data.selected)}}apiGetSelecting(){return this.getSelectionWithData(this.data.selecting)}apiGetSelected(){return this.getSelectionWithData(this.data.selected)}modKeyPressed(t,e){switch(t){case"shift":return e.shiftKey;case"alt":return e.altKey;case"ctrl":return e.ctrlKey}}canSelect(){let t=this.data.enabled;const e=this.pointerData.events.down;return e&&this.data.selectKey&&(t=t&&this.modKeyPressed(this.data.selectKey,e)),t&&(this.data.cells||this.data.items)}getSelectionAreaLocal(){const t={x:0,y:0,width:0,height:0},e=Object.assign({},this.pointerData.initialPosition),i=Object.assign({},this.pointerData.currentPosition),s=i.x-e.x,a=i.y-e.y;return s>=0?(t.x=e.x,t.width=s):(t.x=i.x,t.width=Math.abs(s)),a>=0?(t.y=e.y,t.height=a):(t.y=i.y,t.height=Math.abs(a)),t}translateAreaLocalToGlobal(t){const e=this.state.get("$data.chart.time.leftPx"),i=this.state.get("config.scroll.vertical.posPx");return Object.assign(Object.assign({},t),{x:t.x+e,y:t.y+i})}collectLinkedItems(t,e=this.api.getItemsData()){return[t,...e[t].linkedWith]}getSelectedItem(t){let e,s=this.data.automaticallySelected[i].slice();const a=this.collectLinkedItems(t.id);if(this.data.selected[i].find((e=>e===t.id)))if(e=this.data.selected[i],s.find((e=>e===t.id))){const i=s,a=e.find((e=>t.linkedWith.includes(e)&&!i.includes(e)));s=s.filter((e=>e!==t.id)),s.push(a)}else s=this.data.automaticallySelected[i];else e=this.isMulti()?Array.from(new Set([...this.data.selected[i],...a])):a,s=a.filter((e=>e!==t.id));return e=e.map((e=>(t=this.api.getItem(e),e))),{selected:e,automaticallySelected:s}}isItemVerticallyInsideArea(t,e){if(!e.width||!e.height)return!1;const i=e.y+e.height,s=t.position.viewTop,a=s+t.actualHeight;return s>=e.y&&s<=i||a>=e.y&&a<=i||s>=e.y&&a<=i||s<=e.y&&a>=i}isItemHorizontallyInsideArea(t,e){if(!e.width||!e.height)return!1;const i=e.x+e.width;return t.position.actualLeft>=e.x&&t.position.actualLeft<=i||t.position.actualRight>=e.x&&t.position.actualRight<=i||t.position.actualLeft<=e.x&&t.position.actualRight>=i||t.position.actualLeft>=e.x&&t.position.actualRight<=i}isMulti(){const t=this.pointerData.events.move;return t&&this.data.multiKey&&this.modKeyPressed(this.data.multiKey,t)}getItemsUnderSelectionArea(t){const e=this.state.get("$data.chart.visibleItems"),i=this.api.getItems(e);let s=[];const a=[];for(let e of i){e=this.merge({},e);const i=this.api.getItemData(e.id);if(this.isItemVerticallyInsideArea(i,t)&&this.isItemHorizontallyInsideArea(i,t)){s.find((t=>t===e.id))||s.push(e.id);const t=this.collectLinkedItems(e.id);for(const e of t){const t=this.api.getItem(e);s.find((e=>e===t.id))||(s.push(t.id),a.push(t.id))}}}return s=s.map((t=>(this.api.getItem(t).selected=!0,t))),{selectedItems:s,automaticallySelectedItems:a}}isCellVerticallyInsideArea(t,e){if(!e.width||!e.height)return!1;const i=e.y+e.height,s=t.top,a=s+t.rowData.actualHeight;return s>=e.y&&s<=i||a>=e.y&&a<=i||s>=e.y&&a<=i||s<=e.y&&a>=i}isCellHorizontallyInsideArea(t,e){if(!e.width||!e.height)return!1;const i=e.x+e.width,s=t.time.currentView.leftPx,a=t.time.currentView.rightPx;return s>=e.x&&s<=i||a>=e.x&&a<=i||s<=e.x&&a>=i||s>=e.x&&a<=i}getCellsUnderSelectionArea(t){const e=this.state.get("$data.chart.grid.cells"),i=[];for(const s in e){const a=e[s];this.isCellVerticallyInsideArea(a,t)&&this.isCellHorizontallyInsideArea(a,t)&&(i.find((t=>t===a.id))||i.push(a.id))}return{selectedCells:i}}updateItems(t){if(!this.data.items)return t;const e=this.api.getAllItems(),s=Object.values(e),a=s.filter((t=>t.selecting)).map((t=>t.id)).join("|"),l=this.data.selecting[i].join("|"),n=s.filter((t=>t.selected)).map((t=>t.id)).join("|"),c=this.data.selected[i].join("|");if(a===l&&n===c)return t;t=(t=t.update("config.chart.items.*.selected",!1)).update("config.chart.items.*.selecting",!1);const h=Array.from(new Set([...this.data.selecting[i],...this.data.selected[i]]));for(const s of h){if(!(s in e))continue;const a=this.data.selecting[i].includes(s),l=this.data.selected[i].includes(s);t=(t=t.update(`config.chart.items.${s}.selecting`,a)).update(`config.chart.items.${s}.selected`,l)}return t}updateCells(t){if(!this.data.cells)return t;const i=this.api.getGridCells(),s=this.api.getAllGridCells(),a=i.filter((t=>t.selecting)).map((t=>t.id)).join("|"),l=this.data.selecting[e].join("|"),n=i.filter((t=>t.selected)).map((t=>t.id)).join("|"),c=this.data.selected[e].join("|");return a===l&&n===c||t.update("$data.chart.grid.cells",(t=>{for(const i in t){if(!(i in s))continue;const a=t[i];a.selected=this.data.selected[e].includes(a.id),a.selecting=this.data.selecting[e].includes(a.id)}return t})),t}deselectItems(){if(!this.data.items)return;this.data.selected[i].length=0,this.data.selecting[i].length=0;let t=this.state.multi();t=this.updateItems(t),t.done()}deselectCells(){if(!this.data.cells)return;this.data.selecting[e].length=0,this.data.selected[e].length=0;let t=this.state.multi();t=this.updateCells(t),t.done()}getSelectionWithData(t){const s=this.state.get("config.chart.items"),a=this.state.get("$data.chart.grid.cells");return{[e]:t[e].map((t=>a[t]?a[t]:t)),[i]:t[i].map((t=>s[t]?s[t]:t))}}onSelecting(t,s){const a=this.getSelectionWithData(t),l=this.getSelectionWithData(s);this.started||(this.data.events.onStart(l),this.started=!0,this.api.muteMethod("fullReload"),this.api.muteMethod("prepareExpanded"),this.api.muteMethod("calculateRowsHeight"),this.api.muteMethod("recalculateRowPercents"),this.api.muteMethod("getLastPageRowsHeight"),this.api.muteMethod("calculateVerticalScrollArea"),this.api.muteMethod("calculateVisibleRowsHeights"),this.api.muteMethod("generateVisibleRowsAndItems"),this.api.muteMethod("recalculateTimes"),this.api.muteMethod("heightChange"));const n=this.data.events.onSelecting(a,l);return{[e]:n[e].map((t=>"string"!=typeof t?t.id:t)),[i]:n[i].map((t=>"string"!=typeof t?t.id:t))}}onSelected(t,s){const a=this.getSelectionWithData(t),l=this.getSelectionWithData(s),n=this.data.events.onEnd(a,l);return this.started=!1,this.api.unmuteMethod("heightChange"),this.api.unmuteMethod("recalculateTimes"),this.api.unmuteMethod("generateVisibleRowsAndItems"),this.api.unmuteMethod("calculateVisibleRowsHeights"),this.api.unmuteMethod("calculateVerticalScrollArea"),this.api.unmuteMethod("getLastPageRowsHeight"),this.api.unmuteMethod("recalculateRowPercents"),this.api.unmuteMethod("calculateRowsHeight"),this.api.unmuteMethod("prepareExpanded"),this.api.unmuteMethod("fullReload"),{[e]:n[e].map((t=>"string"!=typeof t?t.id:t)),[i]:n[i].map((t=>"string"!=typeof t?t.id:t))}}updateBodyClass(){this.data.isSelecting?document.body.classList.add(this.data.bodySelectingClassName):document.body.classList.remove(this.data.bodySelectingClassName),this.data.selected[e].length||this.data.selected[i].length?document.body.classList.add(this.data.bodySelectedClassName):document.body.classList.remove(this.data.bodySelectedClassName)}selectMultipleCellsAndItems(){if(!this.canSelect())return;if(!this.data.multipleSelection)return this.deselectItems(),this.deselectCells(),void this.updateData();this.data.isSelecting=!0,this.data.selectionAreaLocal=this.getSelectionAreaLocal(),this.data.selectionAreaGlobal=this.translateAreaLocalToGlobal(this.data.selectionAreaLocal);const t={[e]:[],[i]:[]},s=this.isMulti();if(this.data.cells){const{selectedCells:i}=this.getCellsUnderSelectionArea(this.data.selectionAreaLocal);0===i.length?(t[e].length=0,s||(this.data.selected[e].length=0)):t[e]=i}if(this.data.items){const{selectedItems:e,automaticallySelectedItems:a}=this.getItemsUnderSelectionArea(this.data.selectionAreaLocal);this.data.automaticallySelected[i]=a,0===e.length?(t[i].length=0,s||(this.data.selected[i].length=0)):t[i]=e}if(this.data.cells||this.data.items){this.data.selecting=this.onSelecting(t,this.api.mergeDeep({},this.data.lastSelected));let e=this.state.multi();this.data.cells&&(e=this.updateCells(e)),this.data.items&&(e=this.updateItems(e)),e.done()}}selectItemsIndividually(){if(this.data.isSelecting=!1,this.data.selectionAreaLocal=this.getSelectionAreaLocal(),this.data.currentPosition=this.pointerData.currentPosition,this.data.initialPosition=this.pointerData.initialPosition,!this.data.items)return;if(!this.canSelect())return;const t=this.merge({},this.pointerData.targetData);if(this.data.selected[i].includes(t.id))return;let{selected:s,automaticallySelected:a}=this.getSelectedItem(t);s.length>1&&!this.data.multipleSelection&&(s=[t.id],a=[]),this.isMulti()?t.selected?this.data.selected[i]=s.filter((e=>e!==t.id&&!a.includes(e))):this.data.selected[i]=s:(this.data.selected[i]=s,this.data.selected[e].length=0),this.data.automaticallySelected[i]=a,this.data.selected=this.onSelected(this.api.mergeDeep({},this.data.selected),this.api.mergeDeep({},this.data.lastSelected));let l=this.state.multi();l=this.updateCells(l),l=this.updateItems(l),l.done()}removeMultiUnselected(t){const e=this.data.selected[t].filter((e=>this.data.selecting[t].includes(e))),i=[...this.data.selected[t],...this.data.selecting[t]];return Array.from(new Set(i.filter((t=>!e.includes(t)))))}finishSelection(){if(!this.canSelect())return;let t;t=this.isMulti()?{[e]:this.data.cells?this.removeMultiUnselected(e):[],[i]:this.data.items?this.removeMultiUnselected(i):[]}:{[e]:this.data.cells?[...this.data.selecting[e]]:[],[i]:this.data.items?[...this.data.selecting[i]]:[]},this.data.selected=this.onSelected(t,this.api.mergeDeep({},this.data.lastSelected)),this.data.lastSelected=this.api.mergeDeep({},this.data.selected),this.data.cells&&(this.data.selecting[e].length=0),this.data.items&&(this.data.selecting[i].length=0);let s=this.state.multi();s=this.updateItems(s),s=this.updateCells(s),s.done()}onPointerData(){this.pointerData.isMoving&&this.pointerData.targetType===e&&this.data.rectangularSelection?this.selectMultipleCellsAndItems():this.pointerData.isMoving&&this.pointerData.targetType===e&&!this.data.rectangularSelection?this.deselectItems():this.pointerData.targetType===i?this.selectItemsIndividually():this.pointerData.isMoving||(this.data.isSelecting&&this.finishSelection(),this.data.isSelecting=!1),this.pointerData.isMoving&&this.pointerData.targetType!==e&&this.pointerData.targetType!==i&&this.deselectItems(),this.data.pointerEvents=this.pointerData.events,this.data.pointerState=this.pointerData.pointerState,this.data.targetType=this.pointerData.targetType,this.data.targetData=this.pointerData.targetData,this.updateData(),this.updateBodyClass()}slot(){let t,e=!0;return i=>(e=!0,this.canSelect()&&this.data.isSelecting&&this.data.showOverlay&&this.data.multipleSelection&&this.data.rectangularSelection&&(this.slotStyleMap.style.display="block",this.slotStyleMap.style.left=this.data.selectionAreaLocal.x+"px",this.slotStyleMap.style.top=this.data.selectionAreaLocal.y+"px",this.slotStyleMap.style.width=this.data.selectionAreaLocal.width+"px",this.slotStyleMap.style.height=this.data.selectionAreaLocal.height+"px",e=!1),t=this.html`<div class=${this.slotClassName} style=${this.slotStyleMap}></div>`,this.html`${i}${e?null:t}`)}updateSelectionClassName(t,e){const i="boolean"==typeof e.selected?e.selected:e.item.selected,s="boolean"==typeof e.selecting?e.selecting:e.item.selecting;i?(t.classList.add(this.data.selectedClassName),t.classList.remove(this.data.selectingClassName)):t.classList.remove(this.data.selectedClassName),s?(t.classList.add(this.data.selectingClassName),t.classList.remove(this.data.selectedClassName)):t.classList.remove(this.data.selectingClassName)}selectedAction(t,e){return this.updateSelectionClassName(t,e),{update:this.updateSelectionClassName,destroy:this.updateSelectionClassName}}onCellCreate(t){return t.selected=!!this.data.selected[e].find((e=>e===t.id)),t.selecting=!!this.data.selecting[e].find((e=>e===t.id)),t.content}}t.Plugin=function(t={}){return t=function(t){!function(t){t.events||(t.events={}),t.onStart&&(t.events.onStart=t.onStart),t.onSelecting&&(t.events.onSelecting=t.onSelecting),t.onSelected&&(t.events.onEnd=t.onSelected)}(t);const e={enabled:!0,cells:!0,items:!0,showOverlay:!0,rectangularSelection:!0,multipleSelection:!0,selectedClassName:"gstc__selected",selectingClassName:"gstc__selecting",bodySelectedClassName:"gstc__is-selected",bodySelectingClassName:"gstc__is-selecting",events:{onStart(){},onSelecting:t=>t,onEnd:t=>t}};return t.events=Object.assign(Object.assign({},e.events),t.events),Object.assign(Object.assign({},e),t)}(t),function(e){const i=e.api.mergeDeep,a=e.api;if(!a.isPluginInitialized("TimelinePointer"))throw new Error("TimelinePointer plugin must be initialized before Selection plugin.");const n=e.state.get(s);n&&(t=i({},t,n));const c=new l(e,t);return a.pluginInitialized("Selection"),c.destroy}},Object.defineProperty(t,"__esModule",{value:!0})}));
