"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 COLUMN_DATA_TYPE () {
        return COLUMN_DATA_TYPE;
    },
    get compareFunctionFactory () {
        return compareFunctionFactory;
    }
});
const _sortService = require("../sortService");
const _default = require("../sortFunction/default");
const _mixed = require("../../../helpers/mixed");
function compareFunctionFactory(sortOrder, columnMeta, columnPluginSettings) {
    const checkedTemplate = columnMeta.checkedTemplate;
    const uncheckedTemplate = columnMeta.uncheckedTemplate;
    const { sortEmptyCells } = columnPluginSettings;
    return function(value, nextValue) {
        const isEmptyValue = (0, _mixed.isEmpty)(value);
        const isEmptyNextValue = (0, _mixed.isEmpty)(nextValue);
        const unifiedValue = isEmptyValue ? uncheckedTemplate : value;
        const unifiedNextValue = isEmptyNextValue ? uncheckedTemplate : nextValue;
        const isValueFromTemplate = unifiedValue === uncheckedTemplate || unifiedValue === checkedTemplate;
        const isNextValueFromTemplate = unifiedNextValue === uncheckedTemplate || unifiedNextValue === checkedTemplate;
        // As an empty cell we recognize cells with undefined, null and '' values.
        if (sortEmptyCells === false) {
            if (isEmptyValue && isEmptyNextValue === false) {
                return _sortService.FIRST_AFTER_SECOND;
            }
            if (isEmptyValue === false && isEmptyNextValue) {
                return _sortService.FIRST_BEFORE_SECOND;
            }
        }
        // 1st value === #BAD_VALUE#
        if (isValueFromTemplate === false && isNextValueFromTemplate) {
            return sortOrder === 'asc' ? _sortService.FIRST_BEFORE_SECOND : _sortService.FIRST_AFTER_SECOND;
        }
        // 2nd value === #BAD_VALUE#
        if (isValueFromTemplate && isNextValueFromTemplate === false) {
            return sortOrder === 'asc' ? _sortService.FIRST_AFTER_SECOND : _sortService.FIRST_BEFORE_SECOND;
        }
        // 1st value === #BAD_VALUE# && 2nd value === #BAD_VALUE#
        if (isValueFromTemplate === false && isNextValueFromTemplate === false) {
            // Sorting by values (not just by visual representation).
            return (0, _default.compareFunctionFactory)(sortOrder, columnMeta, columnPluginSettings)(value, nextValue);
        }
        if (unifiedValue === uncheckedTemplate && unifiedNextValue === checkedTemplate) {
            return sortOrder === 'asc' ? _sortService.FIRST_BEFORE_SECOND : _sortService.FIRST_AFTER_SECOND;
        }
        if (unifiedValue === checkedTemplate && unifiedNextValue === uncheckedTemplate) {
            return sortOrder === 'asc' ? _sortService.FIRST_AFTER_SECOND : _sortService.FIRST_BEFORE_SECOND;
        }
        return _sortService.DO_NOT_SWAP;
    };
}
const COLUMN_DATA_TYPE = 'checkbox';
