"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 createArrayAssertion () {
        return createArrayAssertion;
    },
    get intersectValues () {
        return intersectValues;
    },
    get sortComparison () {
        return sortComparison;
    },
    get toEmptyString () {
        return toEmptyString;
    },
    get toVisualValue () {
        return toVisualValue;
    },
    get unifyColumnValues () {
        return unifyColumnValues;
    }
});
const _feature = require("../../helpers/feature");
const sortCompare = (0, _feature.getComparisonFunction)();
function sortComparison(a, b) {
    if (typeof a === 'number' && typeof b === 'number') {
        return a - b;
    }
    return sortCompare(a, b);
}
function toVisualValue(value, defaultEmptyValue) {
    let visualValue = value;
    if (visualValue === '') {
        visualValue = `(${defaultEmptyValue})`;
    }
    return visualValue;
}
function createArrayAssertion(initialData) {
    const dataset = new Set(initialData);
    return function(value) {
        return dataset.has(value);
    };
}
function toEmptyString(value) {
    return value === null || value === undefined ? '' : value;
}
function unifyColumnValues(values) {
    return Array.from(new Set(values)).map((value)=>toEmptyString(value)).sort((a, b)=>{
        if (typeof a === 'number' && typeof b === 'number') {
            return a - b;
        }
        if (a === b) {
            return 0;
        }
        return a > b ? 1 : -1;
    });
}
function intersectValues(base, selected, defaultEmptyValue, callback) {
    const result = [];
    const same = base === selected;
    let selectedItemsAssertion;
    if (!same) {
        selectedItemsAssertion = createArrayAssertion(selected);
    }
    base.forEach((value)=>{
        let checked = false;
        if (same || selectedItemsAssertion(value)) {
            checked = true;
        }
        const item = {
            checked,
            value,
            visualValue: toVisualValue(value, defaultEmptyValue)
        };
        if (callback) {
            callback(item);
        }
        result.push(item);
    });
    return result;
}
