"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVariableColor = exports.toStyle = exports.convertCSSAbsoluteFontSizeToPt = exports.convertAdaptableStyleToCSS = exports.sanitizeStyle = exports.normalizeStyleForAgGrid = exports.AgGridCellStyleProperties = void 0;
const EnumExtensions_1 = require("../Extensions/EnumExtensions");
// required for hacky fix for https://github.com/AdaptableTools/adaptable/issues/2119
exports.AgGridCellStyleProperties = [
    'backgroundColor',
    'color',
    'fontWeight',
    'fontStyle',
    'fontSize',
    'borderColor',
];
// see https://github.com/AdaptableTools/adaptable/issues/2119
// since v29 AG Grid ignores style properties with null or undefined values
// Fix: pass empty string instead of null/undefined
const normalizeStyleForAgGrid = (style) => {
    const normalizedStyle = { ...style };
    exports.AgGridCellStyleProperties.forEach((cellStylePropKey) => {
        if (normalizedStyle[cellStylePropKey] == null) {
            normalizedStyle[cellStylePropKey] = '';
        }
    });
    return normalizedStyle;
};
exports.normalizeStyleForAgGrid = normalizeStyleForAgGrid;
// the inverse of normalizeStyleForAgGrid, see comments above
const sanitizeStyle = (style) => {
    const sanitizedStyle = {};
    Object.keys(style).forEach((key) => {
        if (style[key] != null && style[key] !== '') {
            sanitizedStyle[key] = style[key];
        }
    });
    return sanitizedStyle;
};
exports.sanitizeStyle = sanitizeStyle;
const convertAdaptableStyleToCSS = (style) => {
    let result = {};
    if (style.BackColor !== undefined && style.BackColor !== null) {
        result.backgroundColor = style.BackColor;
    }
    if (style.ForeColor !== undefined && style.ForeColor !== null) {
        result.color = style.ForeColor;
    }
    if (style.FontWeight !== undefined && style.FontWeight !== null) {
        result.fontWeight = style.FontWeight.toLocaleLowerCase();
    }
    if (style.FontStyle !== undefined && style.FontStyle !== null) {
        result.fontStyle = style.FontStyle.toLocaleLowerCase();
    }
    if (style.FontSize !== undefined && style.FontSize !== null) {
        result.fontSize = EnumExtensions_1.EnumExtensions.getCssFontSizeFromFontSizeEnum(style.FontSize);
    }
    if (style.BorderColor !== undefined && style.BorderColor !== null) {
        result.borderColor = style.BorderColor;
    }
    if (style.BorderRadius !== undefined && style.BorderRadius !== null) {
        const preparedRadious = typeof style.BorderRadius === 'number' ? `${style.BorderRadius}px` : style.BorderRadius;
        result.borderRadius = preparedRadious;
    }
    if (style.TextDecoration !== undefined && style.TextDecoration !== null) {
        switch (style.TextDecoration) {
            case 'None':
                break;
            case 'Underline':
                result.textDecoration = 'underline';
                break;
            case 'Overline':
                result.textDecoration = 'overline';
                break;
            case 'LineThrough':
                result.textDecoration = 'line-through';
                break;
        }
    }
    // assertion added to comply with the ag-Grid types
    return result;
};
exports.convertAdaptableStyleToCSS = convertAdaptableStyleToCSS;
// based on http://jerekdain.com/fontconversion.html
const convertCSSAbsoluteFontSizeToPt = (fontSize) => {
    switch (fontSize) {
        case 'x-large':
            return 18;
        case 'large':
            return 13.5;
        case 'medium':
            return 12;
        case 'small':
            return 10;
        case 'x-small':
            return 7;
        default:
            return 12;
    }
};
exports.convertCSSAbsoluteFontSizeToPt = convertCSSAbsoluteFontSizeToPt;
const toStyle = (style) => {
    return {
        ...(0, exports.convertAdaptableStyleToCSS)(style ?? {}),
        borderWidth: style?.BorderColor ? 2 : 0,
        borderStyle: 'solid',
    };
};
exports.toStyle = toStyle;
const getVariableColor = (variable) => {
    const isVariable = variable?.startsWith?.('var(');
    if (!isVariable) {
        return variable;
    }
    const match = /\((.+)\)/.exec(variable);
    if (!match) {
        return variable;
    }
    return getComputedStyle(document.documentElement).getPropertyValue(match[1]).trim();
};
exports.getVariableColor = getVariableColor;
