"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable no-mixed-operators */
/* eslint-disable radix */
/* eslint-disable no-unused-expressions */
const react_1 = __importStar(require("react"));
const lodash_merge_1 = __importDefault(require("lodash.merge"));
const ConfigProvider_1 = require("../utils/ConfigProvider");
const LoadingScreen_1 = __importDefault(require("../components/LoadingScreen"));
const styled_components_1 = require("styled-components");
const color_1 = __importDefault(require("./foundations/color"));
function isLight(color) {
    const hex = color.replace('#', '');
    const cr = parseInt(hex.substr(0, 2), 16);
    const cg = parseInt(hex.substr(2, 2), 16);
    const cb = parseInt(hex.substr(4, 2), 16);
    const brightness = (cr * 299 + cg * 587 + cb * 114) / 1000;
    return brightness > 155;
}
function shadeColor(color, percent) {
    let R = parseInt(color.substring(1, 3), 16);
    let G = parseInt(color.substring(3, 5), 16);
    let B = parseInt(color.substring(5, 7), 16);
    R = parseInt(`${(R * (100 + percent)) / 100}`);
    G = parseInt(`${(G * (100 + percent)) / 100}`);
    B = parseInt(`${(B * (100 + percent)) / 100}`);
    R = R < 255 ? R : 255;
    G = G < 255 ? G : 255;
    B = B < 255 ? B : 255;
    const RR = R.toString(16).length === 1 ? `0${R.toString(16)}` : R.toString(16);
    const GG = G.toString(16).length === 1 ? `0${G.toString(16)}` : G.toString(16);
    const BB = B.toString(16).length === 1 ? `0${B.toString(16)}` : B.toString(16);
    return `#${RR}${GG}${BB}`;
}
const colorToShades = (baseShade, w) => {
    const lighterShade = shadeColor(baseShade, 20 + w);
    const darkerShade = shadeColor(baseShade, -20 - w);
    const darkestShade = shadeColor(baseShade, -30 - w);
    return [lighterShade, baseShade, darkerShade, darkestShade];
};
const processColors = (colors) => {
    const newColors = colors;
    if ((newColors === null || newColors === void 0 ? void 0 : newColors.primary) && typeof newColors.primary === 'string') {
        newColors.primary = colorToShades(newColors.primary, 25);
    }
    if ((newColors === null || newColors === void 0 ? void 0 : newColors.secondary) && typeof colors.secondary === 'string') {
        const secondary = colorToShades(newColors.secondary, 10);
        newColors.secondary = isLight(newColors.secondary)
            ? secondary
            : secondary.reverse();
    }
    if ((newColors === null || newColors === void 0 ? void 0 : newColors.textPrimary) && typeof colors.textPrimary === 'string') {
        const text = colorToShades(newColors.textPrimary, -10);
        newColors.textPrimary = isLight(newColors.textPrimary)
            ? text.reverse()
            : text;
    }
    const weightedColors = { primary: {}, secondary: {}, textPrimary: {} };
    if (newColors === null || newColors === void 0 ? void 0 : newColors.primary) {
        weightedColors.primary = {
            300: newColors.primary[0],
            500: newColors.primary[1],
            700: newColors.primary[2],
            900: newColors.primary[3],
        };
    }
    else
        delete weightedColors.primary;
    if (newColors === null || newColors === void 0 ? void 0 : newColors.secondary) {
        weightedColors.secondary = {
            1000: newColors.secondary[0],
            900: newColors.secondary[1],
            800: newColors.secondary[2],
            700: newColors.secondary[3],
        };
    }
    else
        delete weightedColors.secondary;
    if (newColors === null || newColors === void 0 ? void 0 : newColors.textPrimary) {
        weightedColors.textPrimary = {
            400: newColors.textPrimary[0],
            300: newColors.textPrimary[1],
            200: newColors.textPrimary[2],
            100: newColors.textPrimary[3],
        };
    }
    else
        delete weightedColors.textPrimary;
    if (newColors === null || newColors === void 0 ? void 0 : newColors.videoBackground) {
        Object.assign(weightedColors, {
            videoBackground: newColors.videoBackground,
        });
    }
    return weightedColors;
};
const processTheme = (theme, clientOverrideTheme) => ({
    fonts: {
        heading: '"Inter", sans-serif',
        body: '"Lato", system-ui, sans-serif',
        mono: 'Menlo, monospace',
    },
    colors: lodash_merge_1.default(lodash_merge_1.default(color_1.default, theme === null || theme === void 0 ? void 0 : theme.colors), processColors((clientOverrideTheme === null || clientOverrideTheme === void 0 ? void 0 : clientOverrideTheme.colors) || {})),
    logo: (clientOverrideTheme === null || clientOverrideTheme === void 0 ? void 0 : clientOverrideTheme.logo) || (theme === null || theme === void 0 ? void 0 : theme.logo) || '/dyte_logo.svg',
});
const DyteThemeProvider = ({ children }) => {
    var _a, _b, _c, _d;
    const [theme, _setTheme] = react_1.useState(null);
    const context = react_1.useContext(ConfigProvider_1.ConfigServiceContext);
    const themeConfig = {
        // eslint-disable-next-line react/destructuring-assignment
        logo: (_a = context === null || context === void 0 ? void 0 : context.sessionConfig) === null || _a === void 0 ? void 0 : _a.theme.header.elements.logo,
        colors: ((_b = context === null || context === void 0 ? void 0 : context.sessionConfig) === null || _b === void 0 ? void 0 : _b.theme.colors) || {},
    };
    const setTheme = (newTheme) => {
        _setTheme(processTheme(newTheme, themeConfig));
    };
    react_1.useEffect(() => {
        if ((themeConfig === null || themeConfig === void 0 ? void 0 : themeConfig.colors) || (themeConfig === null || themeConfig === void 0 ? void 0 : themeConfig.logo)) {
            setTheme(themeConfig);
        }
        // eslint-disable-next-line react/destructuring-assignment
    }, [
        (_c = context === null || context === void 0 ? void 0 : context.sessionConfig) === null || _c === void 0 ? void 0 : _c.theme.header.elements.logo,
        (_d = context === null || context === void 0 ? void 0 : context.sessionConfig) === null || _d === void 0 ? void 0 : _d.theme.colors,
    ]);
    if (!theme || !context)
        return react_1.default.createElement(LoadingScreen_1.default, null);
    return react_1.default.createElement(styled_components_1.ThemeProvider, { theme: theme }, children);
};
exports.default = DyteThemeProvider;
//# sourceMappingURL=CustomThemeChakraProvider.js.map