'use strict';

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

const core = require('./core-859ae5a6.js');
const drop_controller = require('./drop.controller-ebab46a4.js');
const utils = require('./utils-2a7c8f6e.js');

const ColumnControl = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.clickListener = (event) => this.onSort(event);
        this.keyListener = utils.enterAsClick(this.onSort.bind(this));
        this.dropListener = (e) => this.onDrop(e);
        this.resizing = false;
        this.anjReorder = core.createEvent(this, "anjReorder", 7);
        this.anjPin = core.createEvent(this, "anjPin", 7);
        this.anjSort = core.createEvent(this, "anjSort", 7);
    }
    async componentWillLoad() {
        this.table = this.column.table;
        this.parentEl = this.element.parentElement;
        this.parentEl.addEventListener('click', this.clickListener);
        this.parentEl.addEventListener('keyup', this.keyListener);
        this.parentEl.addEventListener('anjDrop', this.dropListener);
        if (this.table.orderable && this.column.orderable) {
            this.dragController = new drop_controller.DragController(this.parentEl, { currentIndex: this.columnIndex() });
            this.dropController = new drop_controller.DropController(this.parentEl);
            this.dragController.connect();
            this.dropController.connect();
        }
    }
    componentDidUnload() {
        if (this.dragController) {
            this.dragController.disconnect();
        }
        if (this.dragController) {
            this.dropController.disconnect();
        }
        this.parentEl.removeEventListener('click', this.clickListener);
        this.parentEl.removeEventListener('keyup', this.keyListener);
        this.parentEl.removeEventListener('anjDrop', this.dropListener);
    }
    onSort(event) {
        if (!this.field || this.resizing || !this.column.sortable || fromFilter(event)) {
            return;
        }
        this.anjSort.emit({ field: this.field, order: this.sortIndex > -1 && this.sortOrder === 'asc' ? 'desc' : 'asc' });
    }
    async onContextSelection({ detail }) {
        this.anjPin.emit({ index: this.columnIndex(), pin: detail });
    }
    onResizeStart() {
        this.resizing = true;
    }
    onResizeEnd() {
        this.resizing = false;
    }
    onDrop({ detail }) {
        detail['targetIndex'] = this.columnIndex();
        this.anjReorder.emit(detail);
    }
    columnIndex() {
        return parseInt(this.parentEl.dataset.columnIndex, 10);
    }
    render() {
        return [
            this.column.field && this.column.sortable ?
                core.h("svg", { class: "anj-sort-icon", xmlns: "http://www.w3.org/2000/svg", width: "14", height: "14", viewBox: "0 0 24 24" }, this.sortOrder === 'asc' ? [
                    core.h("path", { fill: "none", d: "M0 0h24v24H0V0z" }),
                    core.h("path", { fill: "currentColor", d: "M4 12l1.41 1.41L11 7.83V20h2V7.83l5.58 5.59L20 12l-8-8-8 8z" })
                ] : [
                    core.h("path", { fill: "none", d: "M0 0h24v24H0V0z" }),
                    core.h("path", { fill: "currentColor", d: "M20 12l-1.41-1.41L13 16.17V4h-2v12.17l-5.58-5.59L4 12l8 8 8-8z" })
                ]) : undefined,
            this.table.sizable && this.column.sizable ? core.h("anj-column-sizer", null) : undefined,
            core.h("anj-context-menu", { attach: this.parentEl, loader: () => this.column.contextMenu() })
        ];
    }
    get element() { return core.getElement(this); }
    static get style() { return "anj-table .anj-header-cell.anj-dragging, anj-table .anj-header-cell.anj-dragover {\n  border-bottom-color: var(--anj-highlight) !important;\n}\nanj-table .anj-header-cell.anj-dragging anj-column-sizer, anj-table .anj-header-cell.anj-dragover anj-column-sizer {\n  display: none;\n}\nanj-table .anj-header-cell.anj-dragging {\n  opacity: 0.5;\n}\nanj-table .anj-header-cell.anj-sorted .anj-sort-icon {\n  opacity: 1;\n  -webkit-transition: opacity 0.3s ease;\n  transition: opacity 0.3s ease;\n}\n\nanj-column-control .anj-sort-icon {\n  color: var(--anj-text-medium);\n  display: inline-block;\n  font-size: 0.625rem;\n  font-weight: bold;\n  line-height: 0.8;\n  opacity: 0;\n  position: relative;\n  top: -4px;\n  -webkit-transition: opacity 0s;\n  transition: opacity 0s;\n}"; }
};
const fromFilter = (event) => {
    if (!event)
        return false;
    return !!event.composedPath().find((e) => e.matches && e.matches('anj-column-filter-toggle'));
};

exports.anj_column_control = ColumnControl;
