"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseLayoutValue = parseLayoutValue;
exports.applyLayoutValue = applyLayoutValue;
exports.applyCalc = applyCalc;
exports.applyPercent = applyPercent;
exports.applyPx = applyPx;
exports.applyMin = applyMin;
exports.applyMax = applyMax;
const array_1 = require("../util/array");
const regex_1 = require("../util/regex");
const string_1 = require("../util/string");
function parseLayoutValue(value) {
    value = (0, string_1.removeWhiteSpace)(value);
    if (value === '' ||
        value === 'auto' ||
        value === 'fit-content' ||
        value === 'none') {
        // throw new Error('layout value not calculable')
        return [0, 0];
    }
    const percentTest = regex_1.REGEX_PERCENT.exec(value);
    if (percentTest) {
        return [0, parseFloat(percentTest[1])];
    }
    else {
        const pxTest = regex_1.REGEX_PX.exec(value);
        if (pxTest) {
            return [parseFloat(pxTest[1]), 0];
        }
        else {
            const calcTest = regex_1.REGEX_CALC.exec(value);
            if (calcTest) {
                return [
                    parseFloat(calcTest[2].replace('+-', '-')),
                    parseFloat(calcTest[1]),
                ];
            }
            else {
                // throw new Error('layout value not recognized')
                return [0, 0];
            }
        }
    }
}
function applyLayoutValue(value, baseValue) {
    value = (0, string_1.removeWhiteSpace)(value);
    if (value === '' ||
        value === 'auto' ||
        value === 'fit-content' ||
        value === 'none') {
        // throw new Error('layout value not calculable')
        return 0;
    }
    const percentTest = regex_1.REGEX_PERCENT.exec(value);
    if (percentTest) {
        return applyPercent(percentTest[1], baseValue);
    }
    const pxTest = regex_1.REGEX_PX.exec(value);
    if (pxTest) {
        return applyPx(pxTest[1], baseValue);
    }
    if (value.startsWith('calc(')) {
        return applyCalc(value.slice(5, -1), baseValue);
    }
    const minTest = value.match(/min\((.+)\)/);
    if (minTest) {
        return applyMin(minTest[1], baseValue);
    }
    const maxTest = value.match(/max\((.+)\)/);
    if (maxTest) {
        return applyMax(maxTest[1], baseValue);
    }
    return applyCalc(value, baseValue);
}
function applyCalc(value, baseValue) {
    let result = 0;
    let last_i = 0;
    let last_sign = '+';
    let open_paren_count = 0;
    for (let i = 0; i < value.length; i++) {
        const char = value[i];
        switch (char) {
            case '(':
                {
                    open_paren_count++;
                }
                break;
            case ')':
                {
                    open_paren_count--;
                }
                break;
            case '+':
            case '-':
            case '*':
            case '/':
                {
                    if (open_paren_count === 0) {
                        const subValue = value.slice(last_i, i) || value;
                        result += applyLayoutValue(subValue, baseValue);
                        last_i = i;
                        last_sign = char;
                    }
                }
                break;
            default: {
                //
            }
        }
    }
    const lastValue = value.slice(last_i);
    if (lastValue !== '') {
        result += applyLayoutValue(lastValue, baseValue);
    }
    return result;
}
function applyPercent(value, baseValue) {
    const p = parseFloat(value);
    return (p / 100) * baseValue;
}
function applyPx(value, baseValue) {
    const p = parseFloat(value);
    return p;
}
function applyMin(value, baseValue) {
    const layoutSegments = value.split(',');
    const layoutValues = layoutSegments.map((segment) => applyLayoutValue(segment, baseValue));
    return (0, array_1.min)(layoutValues);
}
function applyMax(value, baseValue) {
    const layoutSegments = value.split(',');
    const layoutValues = layoutSegments.map((segment) => applyLayoutValue(segment, baseValue));
    return (0, array_1.max)(layoutValues);
}
//# sourceMappingURL=parseLayoutValue.js.map