"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get getTheme () {
        return getTheme;
    },
    get getThemeNames () {
        return getThemeNames;
    },
    get getThemes () {
        return getThemes;
    },
    get hasTheme () {
        return hasTheme;
    },
    get registerTheme () {
        return registerTheme;
    },
    get reinitTheme () {
        return reinitTheme;
    }
});
const _object = require("../helpers/object");
const _console = require("../helpers/console");
const _staticRegister = require("../utils/staticRegister");
const _engine = require("./engine");
const { hasItem, getItem, getNames, getValues, register } = (0, _staticRegister.staticRegister)('themes');
function hasTheme(themeName) {
    return hasItem(themeName);
}
function getTheme(themeName) {
    if (!hasTheme(themeName)) {
        (0, _console.warn)(`Theme "${themeName}" is not registered. Please ensure it is registered before using it.`);
        return undefined;
    }
    return getItem(themeName);
}
function getThemeNames() {
    return getNames();
}
function getThemes() {
    return getValues();
}
/**
 * Parse theme name and config from arguments.
 *
 * @private
 * @param {string|object} themeNameOrConfig Theme name for specific theme or object representing theme config.
 * @param {object} [themeConfig] The theme config object (optional if first parameter has already theme config).
 * @returns {{themeName: string, themeConfigObject: object}} Parsed theme name and config object.
 */ function parseThemeArgs(themeNameOrConfig, themeConfig) {
    let themeName = themeNameOrConfig;
    let themeConfigObject = (0, _object.deepClone)(themeConfig);
    if (typeof themeNameOrConfig === 'string' && (0, _object.isObject)(themeConfig)) {
        themeConfigObject.name = themeNameOrConfig;
    } else if ((0, _object.isObject)(themeNameOrConfig)) {
        themeConfigObject = (0, _object.deepClone)(themeNameOrConfig);
        themeName = themeConfigObject.name;
    }
    return {
        themeName,
        themeConfigObject
    };
}
function registerTheme(themeNameOrConfig, themeConfig) {
    const { themeName, themeConfigObject } = parseThemeArgs(themeNameOrConfig, themeConfig);
    const theme = (0, _engine.createTheme)(themeConfigObject);
    if (hasTheme(themeName)) {
        (0, _console.warn)(`Theme "${themeName}" is already registered. Registration skipped.`);
        return getTheme(themeName);
    }
    register(themeName, theme);
    return theme;
}
function reinitTheme(themeNameOrConfig, themeConfig) {
    const { themeName, themeConfigObject } = parseThemeArgs(themeNameOrConfig, themeConfig);
    if (!hasTheme(themeName)) {
        (0, _console.warn)(`Theme "${themeName}" is not registered. Cannot reinitialize a non-existent theme.`);
        return undefined;
    }
    // Create a new theme with the new config
    const reinitializedTheme = (0, _engine.createTheme)(themeConfigObject);
    // Re-register the theme (this will replace the existing one)
    register(themeName, reinitializedTheme);
    return reinitializedTheme;
}
