"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.configProviderProps = exports.configProviderInjectionKey = void 0;
const vue_1 = require("vue");
const vooks_1 = require("vooks");
const lodash_1 = require("lodash");
const _utils_1 = require("../../_utils");
const _mixins_1 = require("../../_mixins");
exports.configProviderInjectionKey = Symbol('configProviderInjection');
exports.configProviderProps = {
    abstract: Boolean,
    bordered: {
        type: Boolean,
        default: undefined
    },
    clsPrefix: String,
    locale: Object,
    dateLocale: Object,
    namespace: String,
    rtl: Array,
    tag: {
        type: String,
        default: 'div'
    },
    hljs: Object,
    theme: Object,
    themeOverrides: Object,
    componentOptions: Object,
    icons: Object,
    // deprecated
    legacyTheme: String,
    language: {
        type: String,
        validator: () => {
            _utils_1.warn('config-provider', '`language` is deprecated, please use `locale` instead.');
            return true;
        },
        default: undefined
    },
    lang: {
        type: String,
        validator: () => {
            _utils_1.warn('config-provider', '`lang` is deprecated, please use `locale` instead.');
            return true;
        },
        default: undefined
    },
    as: {
        type: String,
        validator: () => {
            _utils_1.warn('config-provider', '`as` is deprecated, please use `tag` instead.');
            return true;
        },
        default: undefined
    },
    themeEnvironment: {
        type: Object,
        validator: () => {
            _utils_1.warn('config-provider', '`theme-environment` is deprecated.');
            return true;
        },
        default: undefined
    },
    themeEnvironments: {
        type: Object,
        validator: () => {
            _utils_1.warn('config-provider', '`theme-environments` is deprecated.');
            return true;
        },
        default: undefined
    }
};
exports.default = vue_1.defineComponent({
    name: 'ConfigProvider',
    alias: ['App'],
    props: exports.configProviderProps,
    setup(props) {
        const NConfigProvider = vue_1.inject(exports.configProviderInjectionKey, null);
        const mergedThemeRef = vue_1.computed(() => {
            const { theme } = props;
            if (theme === null)
                return undefined;
            const inheritedTheme = NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedThemeRef.value;
            return theme === undefined
                ? inheritedTheme
                : inheritedTheme === undefined
                    ? theme
                    : Object.assign({}, inheritedTheme, theme);
        });
        const mergedThemeOverridesRef = vue_1.computed(() => {
            const { themeOverrides } = props;
            // stop inheriting themeOverrides
            if (themeOverrides === null)
                return undefined;
            // use inherited themeOverrides
            if (themeOverrides === undefined) {
                return NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedThemeOverridesRef.value;
            }
            else {
                const inheritedThemeOverrides = NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedThemeOverridesRef.value;
                if (inheritedThemeOverrides === undefined) {
                    // no inherited, use self overrides
                    return themeOverrides;
                }
                else {
                    // merge overrides
                    return lodash_1.merge({}, inheritedThemeOverrides, themeOverrides);
                }
            }
        });
        const mergedNamespaceRef = vooks_1.useMemo(() => {
            const { namespace } = props;
            return namespace === undefined
                ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedNamespaceRef.value
                : namespace;
        });
        const mergedBorderedRef = vooks_1.useMemo(() => {
            const { bordered } = props;
            return bordered === undefined
                ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedBorderedRef.value
                : bordered;
        });
        const mergedIconsRef = vue_1.computed(() => {
            const { icons } = props;
            return icons === undefined ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedIconsRef.value : icons;
        });
        const mergedComponentPropsRef = vue_1.computed(() => {
            const { componentOptions } = props;
            if (componentOptions !== undefined)
                return componentOptions;
            return NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedComponentPropsRef.value;
        });
        const mergedClsPrefixRef = vue_1.computed(() => {
            var _a;
            const { clsPrefix } = props;
            return (_a = NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedClsPrefixRef.value) !== null && _a !== void 0 ? _a : clsPrefix;
        });
        const mergedRtlRef = vue_1.computed(() => {
            const { rtl } = props;
            if (rtl === undefined) {
                return NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedRtlRef.value;
            }
            const rtlEnabledState = {};
            for (const rtlInfo of rtl) {
                rtlEnabledState[rtlInfo.name] = vue_1.markRaw(rtlInfo);
            }
            return rtlEnabledState;
        });
        vue_1.provide(exports.configProviderInjectionKey, {
            mergedRtlRef,
            mergedIconsRef,
            mergedComponentPropsRef,
            mergedBorderedRef,
            mergedNamespaceRef,
            mergedClsPrefixRef,
            mergedLocaleRef: vue_1.computed(() => {
                const { locale } = props;
                if (locale === null)
                    return undefined;
                return locale === undefined
                    ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedLocaleRef.value
                    : locale;
            }),
            mergedDateLocaleRef: vue_1.computed(() => {
                const { dateLocale } = props;
                if (dateLocale === null)
                    return undefined;
                return dateLocale === undefined
                    ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedDateLocaleRef.value
                    : dateLocale;
            }),
            mergedHljsRef: vue_1.computed(() => {
                const { hljs } = props;
                return hljs === undefined ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedHljsRef.value : hljs;
            }),
            mergedThemeRef,
            mergedThemeOverridesRef,
            // deprecated
            mergedLegacyThemeRef: vooks_1.useMemo(() => {
                const { legacyTheme } = props;
                return legacyTheme === undefined
                    ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedLegacyThemeRef.value
                    : legacyTheme;
            }),
            mergedLanguageRef: vooks_1.useMemo(() => {
                const { language, lang } = props;
                return language === undefined
                    ? lang === undefined
                        ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedLanguageRef.value
                        : lang
                    : language;
            }),
            mergedThemeEnvironmentsRef: vue_1.computed(() => {
                const { themeEnvironments, themeEnvironment } = props;
                return themeEnvironments === undefined
                    ? themeEnvironment === undefined
                        ? NConfigProvider === null || NConfigProvider === void 0 ? void 0 : NConfigProvider.mergedThemeEnvironmentsRef.value
                        : themeEnvironment
                    : themeEnvironments;
            })
        });
        return {
            mergedClsPrefix: mergedClsPrefixRef,
            mergedBordered: mergedBorderedRef,
            mergedNamespace: mergedNamespaceRef,
            mergedTheme: mergedThemeRef,
            mergedThemeOverrides: mergedThemeOverridesRef
        };
    },
    render() {
        return !this.abstract
            ? vue_1.h(this.as || this.tag, {
                class: `${this.mergedClsPrefix || _mixins_1.defaultClsPrefix}-config-provider`
            }, vue_1.renderSlot(this.$slots, 'default'))
            : vue_1.renderSlot(this.$slots, 'default');
    }
});
