"use strict";
/*
 * Copyright (c) 2022-2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseFilterValue = void 0;
const constants_1 = require("../constants");
function matchOperator(key, value, position) {
    if (typeof value === 'string') {
        switch (position) {
            case 'start': {
                if (value.substring(0, key.length) === key) {
                    return value.substring(key.length);
                }
                break;
            }
            case 'end': {
                if (value.substring(0 - key.length) === key) {
                    return value.substring(0, value.length - key.length - 1);
                }
                break;
            }
        }
        return undefined;
    }
    if (Array.isArray(value)) {
        let match = false;
        for (let i = 0; i < value.length; i++) {
            const output = matchOperator(key, value[i], position);
            if (typeof output !== 'undefined') {
                match = true;
                value[i] = output;
            }
        }
        if (match) {
            return value;
        }
    }
    return undefined;
}
function parseFilterValue(input) {
    if (typeof input === 'string' &&
        input.includes(constants_1.FilterInputOperatorValue.IN)) {
        input = input.split(constants_1.FilterInputOperatorValue.IN);
    }
    let negation = false;
    let value = matchOperator(constants_1.FilterInputOperatorValue.NEGATION, input, 'start');
    if (typeof value !== 'undefined') {
        negation = true;
        input = value;
    }
    if (Array.isArray(input)) {
        return {
            value: input,
            operator: negation ?
                constants_1.FilterComparisonOperator.NOT_IN :
                constants_1.FilterComparisonOperator.IN,
        };
    }
    value = matchOperator(constants_1.FilterInputOperatorValue.LIKE, input, 'start');
    if (typeof value !== 'undefined') {
        return {
            value,
            operator: negation ?
                constants_1.FilterComparisonOperator.NOT_LIKE :
                constants_1.FilterComparisonOperator.LIKE,
        };
    }
    value = matchOperator(constants_1.FilterInputOperatorValue.LESS_THAN_EQUAL, input, 'start');
    if (typeof value !== 'undefined') {
        return {
            value,
            operator: constants_1.FilterComparisonOperator.LESS_THAN_EQUAL,
        };
    }
    value = matchOperator(constants_1.FilterInputOperatorValue.LESS_THAN, input, 'start');
    if (typeof value !== 'undefined') {
        return {
            value,
            operator: constants_1.FilterComparisonOperator.LESS_THAN,
        };
    }
    value = matchOperator(constants_1.FilterInputOperatorValue.MORE_THAN_EQUAL, input, 'start');
    if (typeof value !== 'undefined') {
        return {
            value,
            operator: constants_1.FilterComparisonOperator.GREATER_THAN_EQUAL,
        };
    }
    value = matchOperator(constants_1.FilterInputOperatorValue.MORE_THAN, input, 'start');
    if (typeof value !== 'undefined') {
        return {
            value,
            operator: constants_1.FilterComparisonOperator.GREATER_THAN,
        };
    }
    return {
        value: input,
        operator: negation ?
            constants_1.FilterComparisonOperator.NOT_EQUAL :
            constants_1.FilterComparisonOperator.EQUAL,
    };
}
exports.parseFilterValue = parseFilterValue;
//# sourceMappingURL=operator.js.map