"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ASC_SORT_STATE () {
        return ASC_SORT_STATE;
    },
    get DESC_SORT_STATE () {
        return DESC_SORT_STATE;
    },
    get HEADER_SPAN_CLASS () {
        return HEADER_SPAN_CLASS;
    },
    get areValidSortStates () {
        return areValidSortStates;
    },
    get createDateTimeCompareFunction () {
        return createDateTimeCompareFunction;
    },
    get createIntlDateCompareFunction () {
        return createIntlDateCompareFunction;
    },
    get createIntlTimeCompareFunction () {
        return createIntlTimeCompareFunction;
    },
    get getHeaderSpanElement () {
        return getHeaderSpanElement;
    },
    get getNextSortOrder () {
        return getNextSortOrder;
    },
    get isFirstLevelColumnHeader () {
        return isFirstLevelColumnHeader;
    },
    get warnAboutPluginsConflict () {
        return warnAboutPluginsConflict;
    },
    get wasHeaderClickedProperly () {
        return wasHeaderClickedProperly;
    }
});
const _moment = /*#__PURE__*/ _interop_require_default(require("moment"));
const _object = require("../../helpers/object");
const _event = require("../../helpers/dom/event");
const _element = require("../../helpers/dom/element");
const _mixed = require("../../helpers/mixed");
const _dateTime = require("../../helpers/dateTime");
const _sortService = require("./sortService");
const _console = require("../../helpers/console");
const _templateLiteralTag = require("../../helpers/templateLiteralTag");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const ASC_SORT_STATE = 'asc';
const DESC_SORT_STATE = 'desc';
const HEADER_SPAN_CLASS = 'colHeader';
/**
 * Get if column state is valid.
 *
 * @param {number} columnState Particular column state.
 * @returns {boolean}
 */ function isValidColumnState(columnState) {
    if ((0, _object.isObject)(columnState) === false) {
        return false;
    }
    const { column, sortOrder } = columnState;
    return Number.isInteger(column) && [
        ASC_SORT_STATE,
        DESC_SORT_STATE
    ].includes(sortOrder);
}
function areValidSortStates(sortStates) {
    if (sortStates.some((columnState)=>isValidColumnState(columnState) === false)) {
        return false;
    }
    const sortedColumns = sortStates.map(({ column })=>column);
    // Indexes occurs only once.
    return new Set(sortedColumns).size === sortedColumns.length;
}
function getNextSortOrder(sortOrder) {
    if (sortOrder === DESC_SORT_STATE) {
        return;
    } else if (sortOrder === ASC_SORT_STATE) {
        return DESC_SORT_STATE;
    }
    return ASC_SORT_STATE;
}
function getHeaderSpanElement(TH) {
    const headerSpanElement = TH.querySelector(`.${HEADER_SPAN_CLASS}`);
    return headerSpanElement;
}
function isFirstLevelColumnHeader(column, TH) {
    if (column < 0) {
        return false;
    }
    return (0, _element.isBottomMostColumnHeader)(TH);
}
function wasHeaderClickedProperly(row, column, clickEvent) {
    if (column < 0 || row >= 0 || (0, _event.isRightClick)(clickEvent)) {
        return false;
    }
    if (row === -1) {
        return true;
    }
    const targetHeader = typeof clickEvent.target.closest === 'function' ? clickEvent.target.closest('th') : null;
    return (0, _element.isBottomMostColumnHeader)(targetHeader);
}
function createDateTimeCompareFunction(sortOrder, format, columnPluginSettings) {
    return function(value, nextValue) {
        const { sortEmptyCells } = columnPluginSettings;
        if (value === nextValue) {
            return _sortService.DO_NOT_SWAP;
        }
        if ((0, _mixed.isEmpty)(value)) {
            if ((0, _mixed.isEmpty)(nextValue)) {
                return _sortService.DO_NOT_SWAP;
            }
            // Just fist value is empty and `sortEmptyCells` option was set
            if (sortEmptyCells) {
                return sortOrder === 'asc' ? _sortService.FIRST_BEFORE_SECOND : _sortService.FIRST_AFTER_SECOND;
            }
            return _sortService.FIRST_AFTER_SECOND;
        }
        if ((0, _mixed.isEmpty)(nextValue)) {
            // Just second value is empty and `sortEmptyCells` option was set
            if (sortEmptyCells) {
                return sortOrder === 'asc' ? _sortService.FIRST_AFTER_SECOND : _sortService.FIRST_BEFORE_SECOND;
            }
            return _sortService.FIRST_BEFORE_SECOND;
        }
        const firstDate = (0, _moment.default)(value, format);
        const nextDate = (0, _moment.default)(nextValue, format);
        if (!firstDate.isValid()) {
            return _sortService.FIRST_AFTER_SECOND;
        }
        if (!nextDate.isValid()) {
            return _sortService.FIRST_BEFORE_SECOND;
        }
        if (nextDate.isAfter(firstDate)) {
            return sortOrder === 'asc' ? _sortService.FIRST_BEFORE_SECOND : _sortService.FIRST_AFTER_SECOND;
        }
        if (nextDate.isBefore(firstDate)) {
            return sortOrder === 'asc' ? _sortService.FIRST_AFTER_SECOND : _sortService.FIRST_BEFORE_SECOND;
        }
        return _sortService.DO_NOT_SWAP;
    };
}
function createIntlDateCompareFunction(sortOrder, format, columnPluginSettings) {
    return function(value, nextValue) {
        const { sortEmptyCells } = columnPluginSettings;
        if (value === nextValue) {
            return _sortService.DO_NOT_SWAP;
        }
        if ((0, _mixed.isEmpty)(value)) {
            if ((0, _mixed.isEmpty)(nextValue)) {
                return _sortService.DO_NOT_SWAP;
            }
            // Just fist value is empty and `sortEmptyCells` option was set
            if (sortEmptyCells) {
                return sortOrder === 'asc' ? _sortService.FIRST_BEFORE_SECOND : _sortService.FIRST_AFTER_SECOND;
            }
            return _sortService.FIRST_AFTER_SECOND;
        }
        if ((0, _mixed.isEmpty)(nextValue)) {
            // Just second value is empty and `sortEmptyCells` option was set
            if (sortEmptyCells) {
                return sortOrder === 'asc' ? _sortService.FIRST_AFTER_SECOND : _sortService.FIRST_BEFORE_SECOND;
            }
            return _sortService.FIRST_BEFORE_SECOND;
        }
        const firstDate = (0, _dateTime.parseToLocalDate)(value);
        const nextDate = (0, _dateTime.parseToLocalDate)(nextValue);
        if (firstDate === null) {
            return _sortService.FIRST_AFTER_SECOND;
        }
        if (nextDate === null) {
            return _sortService.FIRST_BEFORE_SECOND;
        }
        if (nextDate > firstDate) {
            return sortOrder === 'asc' ? _sortService.FIRST_BEFORE_SECOND : _sortService.FIRST_AFTER_SECOND;
        }
        if (nextDate < firstDate) {
            return sortOrder === 'asc' ? _sortService.FIRST_AFTER_SECOND : _sortService.FIRST_BEFORE_SECOND;
        }
        return _sortService.DO_NOT_SWAP;
    };
}
function createIntlTimeCompareFunction(sortOrder, format, columnPluginSettings) {
    return function(value, nextValue) {
        const { sortEmptyCells } = columnPluginSettings;
        if (value === nextValue) {
            return _sortService.DO_NOT_SWAP;
        }
        if ((0, _mixed.isEmpty)(value)) {
            if ((0, _mixed.isEmpty)(nextValue)) {
                return _sortService.DO_NOT_SWAP;
            }
            // Just fist value is empty and `sortEmptyCells` option was set
            if (sortEmptyCells) {
                return sortOrder === 'asc' ? _sortService.FIRST_BEFORE_SECOND : _sortService.FIRST_AFTER_SECOND;
            }
            return _sortService.FIRST_AFTER_SECOND;
        }
        if ((0, _mixed.isEmpty)(nextValue)) {
            // Just second value is empty and `sortEmptyCells` option was set
            if (sortEmptyCells) {
                return sortOrder === 'asc' ? _sortService.FIRST_AFTER_SECOND : _sortService.FIRST_BEFORE_SECOND;
            }
            return _sortService.FIRST_BEFORE_SECOND;
        }
        const firstDate = (0, _dateTime.parseToLocalTime)(value);
        const nextDate = (0, _dateTime.parseToLocalTime)(nextValue);
        if (firstDate === null) {
            return _sortService.FIRST_AFTER_SECOND;
        }
        if (nextDate === null) {
            return _sortService.FIRST_BEFORE_SECOND;
        }
        if (nextDate > firstDate) {
            return sortOrder === 'asc' ? _sortService.FIRST_BEFORE_SECOND : _sortService.FIRST_AFTER_SECOND;
        }
        if (nextDate < firstDate) {
            return sortOrder === 'asc' ? _sortService.FIRST_AFTER_SECOND : _sortService.FIRST_BEFORE_SECOND;
        }
        return _sortService.DO_NOT_SWAP;
    };
}
function warnAboutPluginsConflict(workingPlugin, disabledPlugin) {
    (0, _console.warn)((0, _templateLiteralTag.toSingleLine)`Plugins \`columnSorting\` and \`multiColumnSorting\` should not be enabled simultaneously.\x20
    Only \`${workingPlugin}\` will work. The \`${disabledPlugin}\` plugin will remain disabled.`);
}
