import { colorStringToRGB, rgbaToHex } from "../util/color";

export function getCellFont(cellStyle, cellType) {
    return {
        name: getFirstFontFromFontFamily(cellStyle.fontFamily) || "Arial",
        size: cellStyle.fontSize || 10,
        bold: isFontBold(cellStyle.fontWeight),
        italic: "italic" === cellStyle.fontStyle,
        color: getColor("link" === cellType ? cellStyle._linkColor : cellStyle.color),
        underline: cellStyle.underline
    };
}

function isFontBold(fontWeight) {
    return "number" == typeof fontWeight ? fontWeight >= 600 : Number(fontWeight) >= 600 || "bold" === fontWeight;
}

function getFirstFontFromFontFamily(fontFamily) {
    return fontFamily.split(",").map((font => font.trim()))[0];
}

export function getCellFill(cellStyle) {
    return {
        type: "pattern",
        pattern: "solid",
        fgColor: getColor(cellStyle.bgColor)
    };
}

export function getCellBorder(cellStyle) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const {borderColor: borderColor, borderLineWidth: borderLineWidth, borderLineDash: borderLineDash, _strokeArrayWidth: _strokeArrayWidth, _strokeArrayColor: _strokeArrayColor} = cellStyle;
    if (_strokeArrayColor || _strokeArrayWidth) {
        const border = {};
        return _strokeArrayColor && !_strokeArrayColor[0] || _strokeArrayWidth && !_strokeArrayWidth[0] || (border.top = {
            style: getBorderStyle(null !== (_a = null == _strokeArrayWidth ? void 0 : _strokeArrayWidth[0]) && void 0 !== _a ? _a : borderLineWidth, borderLineDash),
            color: getColor(null !== (_b = null == _strokeArrayColor ? void 0 : _strokeArrayColor[0]) && void 0 !== _b ? _b : borderColor)
        }), _strokeArrayColor && !_strokeArrayColor[1] || _strokeArrayWidth && !_strokeArrayWidth[1] || (border.right = {
            style: getBorderStyle(null !== (_c = null == _strokeArrayWidth ? void 0 : _strokeArrayWidth[1]) && void 0 !== _c ? _c : borderLineWidth, borderLineDash),
            color: getColor(null !== (_d = null == _strokeArrayColor ? void 0 : _strokeArrayColor[1]) && void 0 !== _d ? _d : borderColor)
        }), _strokeArrayColor && !_strokeArrayColor[2] || _strokeArrayWidth && !_strokeArrayWidth[2] || (border.bottom = {
            style: getBorderStyle(null !== (_e = null == _strokeArrayWidth ? void 0 : _strokeArrayWidth[2]) && void 0 !== _e ? _e : borderLineWidth, borderLineDash),
            color: getColor(null !== (_f = null == _strokeArrayColor ? void 0 : _strokeArrayColor[2]) && void 0 !== _f ? _f : borderColor)
        }), _strokeArrayColor && !_strokeArrayColor[3] || _strokeArrayWidth && !_strokeArrayWidth[3] || (border.left = {
            style: getBorderStyle(null !== (_g = null == _strokeArrayWidth ? void 0 : _strokeArrayWidth[3]) && void 0 !== _g ? _g : borderLineWidth, borderLineDash),
            color: getColor(null !== (_h = null == _strokeArrayColor ? void 0 : _strokeArrayColor[3]) && void 0 !== _h ? _h : borderColor)
        }), border;
    }
    if (0 === borderLineWidth) return {};
    const border = {
        style: getBorderStyle(borderLineWidth, borderLineDash),
        color: getColor(borderColor)
    };
    return {
        top: border,
        left: border,
        bottom: border,
        right: border
    };
}

function getBorderStyle(lineWidth, borderLineDash) {
    return borderLineDash && borderLineDash.length ? lineWidth <= 2 ? "dashed" : "mediumDashed" : lineWidth <= .5 ? "hair" : lineWidth <= 1 ? "thin" : lineWidth <= 2 ? "medium" : "thick";
}

export function getCellAlignment(cellStyle, indent) {
    return {
        horizontal: cellStyle.textAlign || "left",
        vertical: cellStyle.textBaseline || "middle",
        wrapText: cellStyle.autoWrapText || !1,
        indent: indent || 0
    };
}

function getColor(color) {
    return {
        argb: rgbaToHex(colorStringToRGB(color))
    };
}
//# sourceMappingURL=style.js.map
