'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const core = require('./core-b7b9d752.js');

const MIN_CELL_WIDTH = 40;
const ColumnSizer = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.resizeListener = ({ movementX }) => this.onResize(movementX);
        this.resizeEndListener = () => this.onResizeEnd();
        this.anjResizeStart = core.createEvent(this, "anjResizeStart", 7);
        this.anjResizeEnd = core.createEvent(this, "anjResizeEnd", 7);
    }
    componentDidLoad() {
        this.parentHeader = this.element.parentElement.parentElement;
        this.table = this.element.closest('table');
    }
    onMousedown(e) {
        e.preventDefault();
    }
    onMouseDown(e) {
        e.preventDefault();
        e.stopPropagation();
        this.column = this.findColumn(this.parentHeader);
        this.siblingHeader = this.findSiblingHeader(this.column);
        this.siblingColumn = this.findColumn(this.siblingHeader);
        this.width = this.parentHeader.offsetWidth;
        this.siblingWidth = this.siblingHeader.offsetWidth;
        this.setDisplayHeight(this.table.parentElement);
        this.anjResizeStart.emit();
        this.table.addEventListener('mousemove', this.resizeListener);
        this.table.addEventListener('mouseup', this.resizeEndListener);
    }
    onResizeEnd() {
        this.table.removeEventListener('mousemove', this.resizeListener);
        this.table.removeEventListener('mouseup', this.resizeEndListener);
        return new Promise(resolve => {
            window.setTimeout(() => {
                this.setDisplayHeight();
                this.anjResizeEnd.emit();
                resolve();
            });
        });
    }
    onResize(delta) {
        if (delta === 0) {
            return;
        }
        this.width = this.column.pinned === 'right' ? this.width - delta : this.width + delta;
        this.siblingWidth = this.column.pinned === 'right' ? this.siblingWidth + delta : this.siblingWidth - delta;
        if (this.width < MIN_CELL_WIDTH || this.siblingWidth < MIN_CELL_WIDTH) {
            return;
        }
        this.column.setWidth(`${this.width}px`);
        this.siblingColumn.setWidth(`${this.siblingWidth}px`);
        if (this.siblingColumn.pinned === this.column.pinned) {
            this.siblingColumn.setOffset(`${this.width}px`);
        }
    }
    findColumn(header) {
        return header.querySelector('anj-column-control').column;
    }
    findSiblingHeader(column) {
        return (column.pinned === 'right' ? this.parentHeader.previousSibling : this.parentHeader.nextSibling);
    }
    setDisplayHeight(ref) {
        this.element.style.height = ref ? `${ref.offsetHeight}px` : '100%';
    }
    get element() { return core.getElement(this); }
    static get style() { return "anj-column-sizer {\n  background-color: var(--anj-border);\n  cursor: col-resize;\n  display: block;\n  height: 100%;\n  position: absolute;\n  right: 0;\n  opacity: 0;\n  top: 0;\n  -webkit-transition: opacity 0.3s ease;\n  transition: opacity 0.3s ease;\n  width: 0.25rem;\n  z-index: 3;\n}\nanj-column-sizer:active {\n  background-color: var(--anj-highlight);\n  opacity: 0.5;\n}\n\nanj-table .anj-header-cell:last-child:not(.anj-pinned-right) anj-column-sizer {\n  display: none;\n}\nanj-table .anj-header-cell:hover anj-column-sizer {\n  opacity: 0.5;\n}\nanj-table .anj-header-cell.anj-pinned-right anj-column-sizer {\n  left: 0;\n  right: auto;\n}\nanj-table .anj-header-cell.anj-pinned-right ~ .anj-pinned-right {\n  right: 0;\n}"; }
};

exports.anj_column_sizer = ColumnSizer;
