"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCheck = void 0;
const vue_1 = require("vue");
const _utils_1 = require("../../_utils");
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function useCheck(props, data) {
    const { paginatedDataRef, treeMateRef, selectionColumnRef } = data;
    const uncontrolledCheckedRowKeysRef = vue_1.ref(props.defaultCheckedRowKeys);
    const mergedCheckState = vue_1.computed(() => {
        const { checkedRowKeys } = props;
        return treeMateRef.value.getCheckedKeys(checkedRowKeys === undefined
            ? uncontrolledCheckedRowKeysRef.value
            : checkedRowKeys, {
            cascade: props.cascade
        });
    });
    const mergedCheckedRowKeysRef = vue_1.computed(() => mergedCheckState.value.checkedKeys);
    const mergedInderminateRowKeysRef = vue_1.computed(() => mergedCheckState.value.indeterminateKeys);
    const mergedCheckedRowKeySetRef = vue_1.computed(() => {
        return new Set(mergedCheckedRowKeysRef.value);
    });
    const mergedInderminateRowKeySetRef = vue_1.computed(() => {
        return new Set(mergedInderminateRowKeysRef.value);
    });
    const countOfCurrentPageCheckedRowsRef = vue_1.computed(() => {
        const { value: mergedCheckedRowKeySet } = mergedCheckedRowKeySetRef;
        return paginatedDataRef.value.reduce((total, tmNode) => {
            const { key } = tmNode;
            return total + (mergedCheckedRowKeySet.has(key) ? 1 : 0);
        }, 0);
    });
    const someRowsCheckedRef = vue_1.computed(() => {
        const { value: mergedInderminateRowKeySet } = mergedInderminateRowKeySetRef;
        return ((countOfCurrentPageCheckedRowsRef.value > 0 &&
            countOfCurrentPageCheckedRowsRef.value <
                paginatedDataRef.value.length) ||
            paginatedDataRef.value.some((rowData) => mergedInderminateRowKeySet.has(rowData.key)));
    });
    const allRowsCheckedRef = vue_1.computed(() => {
        const { length } = paginatedDataRef.value;
        return length !== 0 && countOfCurrentPageCheckedRowsRef.value === length;
    });
    const headerCheckboxDisabledRef = vue_1.computed(() => {
        return paginatedDataRef.value.length === 0;
    });
    function doUpdateCheckedRowKeys(keys) {
        const { 'onUpdate:checkedRowKeys': _onUpdateCheckedRowKeys, onUpdateCheckedRowKeys, onCheckedRowKeysChange } = props;
        if (_onUpdateCheckedRowKeys)
            _utils_1.call(_onUpdateCheckedRowKeys, keys);
        if (onUpdateCheckedRowKeys)
            _utils_1.call(onUpdateCheckedRowKeys, keys);
        if (onCheckedRowKeysChange)
            _utils_1.call(onCheckedRowKeysChange, keys);
        uncontrolledCheckedRowKeysRef.value = keys;
    }
    function doCheck(rowKey) {
        doUpdateCheckedRowKeys(treeMateRef.value.check(rowKey, mergedCheckedRowKeysRef.value, {
            cascade: props.cascade
        }).checkedKeys);
    }
    function doUncheck(rowKey) {
        doUpdateCheckedRowKeys(treeMateRef.value.uncheck(rowKey, mergedCheckedRowKeysRef.value, {
            cascade: props.cascade
        }).checkedKeys);
    }
    function doCheckAll(checkWholeTable = false) {
        const { value: column } = selectionColumnRef;
        if (!column)
            return;
        const rowKeysToCheck = [];
        (checkWholeTable
            ? treeMateRef.value.treeNodes
            : paginatedDataRef.value).forEach((tmNode) => {
            if (!tmNode.disabled) {
                rowKeysToCheck.push(tmNode.key);
            }
        });
        // alway cascade, to emit correct row keys
        doUpdateCheckedRowKeys(treeMateRef.value.check(rowKeysToCheck, mergedCheckedRowKeysRef.value, {
            cascade: true
        }).checkedKeys);
    }
    function doUncheckAll(checkWholeTable = false) {
        const { value: column } = selectionColumnRef;
        if (!column)
            return;
        const rowKeysToUncheck = [];
        (checkWholeTable
            ? treeMateRef.value.treeNodes
            : paginatedDataRef.value).forEach((tmNode) => {
            if (!tmNode.disabled) {
                rowKeysToUncheck.push(tmNode.key);
            }
        });
        // alway cascade, to emit correct row keys
        doUpdateCheckedRowKeys(treeMateRef.value.uncheck(rowKeysToUncheck, mergedCheckedRowKeysRef.value, {
            cascade: true
        }).checkedKeys);
    }
    return {
        mergedCheckedRowKeySetRef,
        mergedCheckedRowKeysRef,
        mergedInderminateRowKeySetRef,
        someRowsCheckedRef,
        allRowsCheckedRef,
        headerCheckboxDisabledRef,
        doUpdateCheckedRowKeys,
        doCheckAll,
        doUncheckAll,
        doCheck,
        doUncheck
    };
}
exports.useCheck = useCheck;
