import i18n from "../global/i18n";
export function format(first, middle, last) {
    return (first || '') + (middle ? ` ${middle}` : '') + (last ? ` ${last}` : '');
}
export function useTranslation() {
    return i18n.t;
}
export const rgbToHex = (rgb) => {
    const values = rgb.match(/\d+/g) || [];
    const hexValues = values.map(val => {
        const hex = parseInt(val, 10).toString(16);
        return hex.length === 1 ? '0' + hex : hex;
    });
    return '#' + hexValues.join('');
};
export const calculateBrightness = (hexColor) => {
    // Formula for calculating brightness: 0.299*R + 0.587*G + 0.114*B
    const r = parseInt(hexColor.substring(1, 3), 16);
    const g = parseInt(hexColor.substring(3, 5), 16);
    const b = parseInt(hexColor.substring(5, 7), 16);
    return 0.299 * r + 0.587 * g + 0.114 * b;
};
export const getContrastColorHelper = (color, isInvert = false) => {
    let hexColor = '#000';
    const format = color.includes('rgb') ? 'rgb' : 'hex';
    if (format === 'hex') {
        hexColor = color;
    }
    else if (format === 'rgb') {
        hexColor = rgbToHex(color);
    }
    const brightness = calculateBrightness(hexColor);
    if (isInvert) {
        return brightness > 128 ? '#fff' : '#000';
    }
    return brightness > 128 ? '#000' : '#fff';
};
export function getRandomHexColor() {
    const hex = Math.floor(Math.random() * 0xffffff).toString(16);
    return `#${hex.padStart(6, '0')}`;
}
export function base64ToFileList(base64Array, filenames) {
    const dataTransfer = new DataTransfer();
    base64Array.forEach((base64, index) => {
        const arr = base64.split(',');
        const mimeMatch = arr[0].match(/:(.*?);/);
        const mime = mimeMatch ? mimeMatch[1] : 'image/png';
        const bstr = atob(arr[1]);
        let n = bstr.length;
        const u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        const fileName = filenames && filenames.length > index ? filenames[index] : `image${index}.png`;
        const file = new File([u8arr], fileName, { type: mime });
        dataTransfer.items.add(file);
    });
    return dataTransfer.files;
}
export const preventInputNoneBasicNumber = (event) => {
    return (['e', 'E', '+', '-', 'ArrowDown'].includes(event.key) || ((event.key === 'ArrowDown') && ((parseInt(event.target.value) === 0) || (event.target.value === '')))) && event.preventDefault();
};
export const preventInputNoneBasicNumberAndDot = (event) => {
    return (['e', 'E', '+', '-', '.'].includes(event.key) || ((event.key === 'ArrowDown') && ((parseInt(event.target.value) === 0) || (event.target.value === '')))) && event.preventDefault();
};
