"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vue_1 = require("vue");
const seemly_1 = require("seemly");
const _utils_1 = require("../../../_utils");
const checkbox_1 = require("../../../checkbox");
const ellipsis_1 = require("../../../ellipsis");
const SortButton_1 = require("../HeaderButton/SortButton");
const FilterButton_1 = require("../HeaderButton/FilterButton");
const utils_1 = require("../utils");
const interface_1 = require("../interface");
const SelectionMenu_1 = require("./SelectionMenu");
function renderTitle(column) {
    return typeof column.title === 'function'
        ? column.title(column)
        : column.title;
}
exports.default = vue_1.defineComponent({
    name: 'DataTableHeader',
    props: {
        discrete: {
            type: Boolean,
            default: true
        }
    },
    setup() {
        const { mergedClsPrefixRef, scrollXRef, fixedColumnLeftMapRef, fixedColumnRightMapRef, mergedCurrentPageRef, allRowsCheckedRef, someRowsCheckedRef, rowsRef, colsRef, mergedThemeRef, checkOptionsRef, mergedSortStateRef, componentId, scrollPartRef, mergedTableLayoutRef, headerCheckboxDisabledRef, handleTableHeaderScroll, doUpdateSorter, doUncheckAll, doCheckAll
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         } = vue_1.inject(interface_1.dataTableInjectionKey);
        function handleCheckboxUpdateChecked() {
            if (someRowsCheckedRef.value || allRowsCheckedRef.value) {
                doUncheckAll();
            }
            else {
                doCheckAll();
            }
        }
        function handleColHeaderClick(e, column) {
            if (seemly_1.happensIn(e, 'dataTableFilter'))
                return;
            if (!utils_1.isColumnSortable(column))
                return;
            const activeSorter = mergedSortStateRef.value;
            const nextSorter = utils_1.createNextSorter(column, activeSorter);
            doUpdateSorter(nextSorter);
        }
        function handleMouseenter() {
            scrollPartRef.value = 'head';
        }
        return {
            componentId,
            mergedSortState: mergedSortStateRef,
            mergedClsPrefix: mergedClsPrefixRef,
            scrollX: scrollXRef,
            fixedColumnLeftMap: fixedColumnLeftMapRef,
            fixedColumnRightMap: fixedColumnRightMapRef,
            currentPage: mergedCurrentPageRef,
            allRowsChecked: allRowsCheckedRef,
            someRowsChecked: someRowsCheckedRef,
            rows: rowsRef,
            cols: colsRef,
            mergedTheme: mergedThemeRef,
            checkOptions: checkOptionsRef,
            mergedTableLayout: mergedTableLayoutRef,
            headerCheckboxDisabled: headerCheckboxDisabledRef,
            handleMouseenter,
            handleCheckboxUpdateChecked,
            handleColHeaderClick,
            handleTableHeaderScroll
        };
    },
    render() {
        const { mergedClsPrefix, fixedColumnLeftMap, fixedColumnRightMap, currentPage, allRowsChecked, someRowsChecked, mergedSortState, rows, cols, mergedTheme, checkOptions, componentId, discrete, mergedTableLayout, headerCheckboxDisabled, handleColHeaderClick, handleCheckboxUpdateChecked } = this;
        let hasEllipsis = false;
        const theadVNode = (vue_1.h("thead", { class: `${mergedClsPrefix}-data-table-thead`, "data-n-id": componentId }, rows.map((row) => {
            return (vue_1.h("tr", { class: `${mergedClsPrefix}-data-table-tr` }, row.map(({ column, colSpan, rowSpan, isLast }) => {
                const key = utils_1.getColKey(column);
                const { ellipsis } = column;
                if (!hasEllipsis && ellipsis)
                    hasEllipsis = true;
                return (vue_1.h("th", { key: key, style: {
                        textAlign: column.align,
                        left: seemly_1.pxfy(fixedColumnLeftMap[key]),
                        right: seemly_1.pxfy(fixedColumnRightMap[key])
                    }, colspan: colSpan, rowspan: rowSpan, "data-col-key": key, class: [
                        `${mergedClsPrefix}-data-table-th`,
                        column.fixed &&
                            `${mergedClsPrefix}-data-table-th--fixed-${column.fixed}`,
                        {
                            [`${mergedClsPrefix}-data-table-th--hover`]: (mergedSortState === null || mergedSortState === void 0 ? void 0 : mergedSortState.order) &&
                                mergedSortState.columnKey === key,
                            [`${mergedClsPrefix}-data-table-th--filterable`]: utils_1.isColumnFilterable(column),
                            [`${mergedClsPrefix}-data-table-th--sortable`]: utils_1.isColumnSortable(column),
                            [`${mergedClsPrefix}-data-table-th--selection`]: column.type === 'selection',
                            [`${mergedClsPrefix}-data-table-th--last`]: isLast
                        },
                        column.className
                    ], onClick: column.type !== 'selection' &&
                        column.type !== 'expand' &&
                        !('children' in column)
                        ? (e) => {
                            handleColHeaderClick(e, column);
                        }
                        : undefined },
                    column.type === 'selection' ? (vue_1.h(vue_1.Fragment, null,
                        vue_1.h(checkbox_1.NCheckbox, { key: currentPage, privateTableHeader: true, checked: allRowsChecked, indeterminate: someRowsChecked, disabled: headerCheckboxDisabled, onUpdateChecked: handleCheckboxUpdateChecked }),
                        checkOptions ? (vue_1.h(SelectionMenu_1.default, { clsPrefix: mergedClsPrefix })) : null)) : ellipsis === true || (ellipsis && !ellipsis.tooltip) ? (vue_1.h("div", { class: `${mergedClsPrefix}-data-table-th__ellipsis` }, renderTitle(column))) // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
                        : ellipsis && ellipsis.tooltip ? (vue_1.h(ellipsis_1.NEllipsis, { tooltip: ellipsis.tooltip, theme: mergedTheme.peers.Ellipsis, themeOverrides: mergedTheme.peerOverrides.Ellipsis }, {
                            default: () => renderTitle(column)
                        })) : (renderTitle(column)),
                    utils_1.isColumnSortable(column) ? (vue_1.h(SortButton_1.default, { column: column })) : null,
                    utils_1.isColumnFilterable(column) ? (vue_1.h(FilterButton_1.default, { column: column, options: column.filterOptions })) : null));
            })));
        })));
        if (!discrete) {
            return theadVNode;
        }
        const { handleTableHeaderScroll, handleMouseenter, scrollX } = this;
        return (vue_1.h("div", { class: `${mergedClsPrefix}-data-table-base-table-header`, onScroll: handleTableHeaderScroll, onMouseenter: handleMouseenter },
            vue_1.h("table", { ref: "body", class: `${mergedClsPrefix}-data-table-table`, style: {
                    minWidth: _utils_1.formatLength(scrollX),
                    tableLayout: mergedTableLayout
                } },
                vue_1.h("colgroup", null, cols.map((col) => (vue_1.h("col", { key: col.key, style: col.style })))),
                theadVNode)));
    }
});
