import { convertPxToPt, convertPtToPx, fontFamilyList, fontSizeList } from './ExcelFile/ExcelFileComponents/util.js';

const getTextAlignment = (alignment) => {
    switch (alignment.toUpperCase()) {
        case 'LEFT':
            return 'left';
        case 'RIGHT':
            return 'right';
        case 'CENTER':
            return 'center';
        case 'JUSTIFY':
            return 'justify';
        case 'BOTTOM':
            return 'end';
        case 'TOP':
            return 'start';
        default:
            return 'left';
    }
};
const getTextAlignmentBack = (alignment) => {
    switch (alignment) {
        case 'left':
            return 'LEFT';
        case 'right':
            return 'RIGHT';
        case 'center':
            return 'CENTER';
        case 'justify':
            return 'JUSTIFY';
        case 'end':
            return 'BOTTOM';
        case 'start':
            return 'TOP';
        default:
            return 'BOTTOM';
    }
};
const getFontFamily = (selectedFontType) => {
    const fontFamilyExists = fontFamilyList.some((element) => {
        return element.label === selectedFontType;
    });
    if (!fontFamilyExists) {
        return 'Poppins';
    }
    return selectedFontType;
};
const closestFontSize = (fontSize) => fontSizeList.reduce((closest, current) => {
    return Math.abs(current - parseFloat(fontSize)) <
        Math.abs(closest - parseFloat(fontSize))
        ? current
        : closest;
});
function convertStyleToFrontend(backendStyle, minimumColumnWidth = 100) {
    const { border } = backendStyle;
    let borderTop = '';
    let borderRight = '';
    let borderBottom = '';
    let borderLeft = '';
    if (border.top !== 'THIN') {
        borderTop =
            border.top === 'MEDIUM'
                ? '2px solid var(--input-hover-border-color)'
                : '1px solid var(--excel-header-border)';
    }
    if (border.right !== 'THIN') {
        borderRight =
            border.right === 'MEDIUM'
                ? '2px solid var(--input-hover-border-color)'
                : '1px solid var(--excel-header-border)';
    }
    if (border.bottom !== 'THIN') {
        borderBottom =
            border.bottom === 'MEDIUM'
                ? '2px solid var(--input-hover-border-color)'
                : '1px solid var(--excel-header-border)';
    }
    if (border.left !== 'THIN') {
        borderLeft =
            border.left === 'MEDIUM'
                ? '2px solid var(--input-hover-border-color)'
                : '1px solid var(--excel-header-border)';
    }
    return {
        fontSize: convertPtToPx(closestFontSize(String(backendStyle.size))),
        fontFamily: getFontFamily(backendStyle.name),
        color: `#${backendStyle.color}`,
        backgroundColor: `#${backendStyle.backgroundColor}`,
        fontWeight: backendStyle.bold ? 'bold' : 'normal',
        fontStyle: backendStyle.italic ? 'italic' : 'normal',
        textDecoration: backendStyle.underline ? 'underline' : 'normal',
        borderTop: borderTop,
        borderRight: borderRight,
        borderBottom: borderBottom,
        borderLeft: borderLeft,
        textAlign: getTextAlignment(backendStyle.alignment.horizontal),
        alignContent: getTextAlignment(backendStyle.alignment.vertical),
        height: backendStyle.height ?? 32,
        width: backendStyle.width ?? minimumColumnWidth,
    };
}
const getBorderStyle = (border) => {
    if (!border || border === 'none') {
        return 'THIN';
    }
    if (border.includes('solid') ||
        border.includes('dashed') ||
        border.includes('dotted')) {
        if (border.includes('2px') || border.includes('medium')) {
            return 'MEDIUM';
        }
        else if (border.includes('3px') || border.includes('medium')) {
            return 'THICK';
        }
    }
    return 'THIN';
};
const convertStyleToBackend = (frontendStyle, minimumColumnWidth = 100) => {
    return {
        name: frontendStyle.fontFamily || 'Arial',
        size: convertPxToPt(frontendStyle.fontSize?.toString() || '12'),
        bold: frontendStyle.fontWeight === 'bold',
        italic: frontendStyle.fontStyle === 'italic',
        underline: frontendStyle.textDecoration === 'underline',
        color: frontendStyle.color?.replace('#', '') || '000000',
        backgroundColor: frontendStyle.backgroundColor?.replace('#', '') || 'FFFFFF',
        borderColor: 'F2F2F2',
        border: {
            top: getBorderStyle(frontendStyle.borderTop ? frontendStyle.borderTop.toString() : undefined),
            right: getBorderStyle(frontendStyle.borderRight
                ? frontendStyle.borderRight.toString()
                : undefined),
            bottom: getBorderStyle(frontendStyle.borderBottom
                ? frontendStyle.borderBottom.toString()
                : undefined),
            left: getBorderStyle(frontendStyle.borderLeft
                ? frontendStyle.borderLeft.toString()
                : undefined),
        },
        alignment: {
            horizontal: getTextAlignmentBack(frontendStyle.textAlign),
            vertical: getTextAlignmentBack(frontendStyle.alignContent),
            wrapText: false,
        },
        width: frontendStyle.width || minimumColumnWidth,
        height: frontendStyle.height || 32,
    };
};

export { convertStyleToBackend, convertStyleToFrontend };
//# sourceMappingURL=dataConversion.js.map
