"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var vdom_1 = require("@dojo/framework/core/vdom");
var theme_1 = require("@dojo/framework/core/middleware/theme");
var ThemeInjector_1 = require("@dojo/framework/core/ThemeInjector");
var factory = vdom_1.create({ coreTheme: theme_1.default }).properties();
exports.THEME_KEY = ' _key';
function uppercaseFirstChar(value) {
    return "" + value.charAt(0).toUpperCase() + value.slice(1);
}
function lowercaseFirstChar(value) {
    return "" + value.charAt(0).toLowerCase() + value.slice(1);
}
function isThemeWithVariant(theme) {
    return theme && theme.hasOwnProperty('variant');
}
exports.theme = factory(function (_a) {
    var coreTheme = _a.middleware.coreTheme, properties = _a.properties;
    var coreVariant = coreTheme.variant, get = coreTheme.get, set = coreTheme.set, classes = coreTheme.classes;
    function getTheme() {
        var theme = properties().theme;
        if (theme) {
            return theme;
        }
        var themePayload = coreTheme.get();
        if (ThemeInjector_1.isThemeInjectorPayloadWithVariant(themePayload)) {
            return { theme: themePayload.theme, variant: themePayload.variant };
        }
        else if (themePayload) {
            return themePayload.theme;
        }
    }
    return {
        compose: function (baseCss, css, prefix) {
            var _a, _b, _c, _d, _e;
            var theme = getTheme();
            var baseKey = baseCss[exports.THEME_KEY];
            var variantKey = css[exports.THEME_KEY];
            var virtualCss = Object.keys(baseCss).reduce(function (virtualCss, key) {
                if (key === exports.THEME_KEY) {
                    return virtualCss;
                }
                if (prefix && !virtualCss["" + prefix + uppercaseFirstChar(key)]) {
                    virtualCss["" + prefix + uppercaseFirstChar(key)] = ' ';
                }
                if (!css[key]) {
                    virtualCss[key] = ' ';
                }
                return virtualCss;
            }, (_a = {}, _a[exports.THEME_KEY] = variantKey, _a));
            var virtualTheme = coreTheme.classes(virtualCss);
            var variantTheme = coreTheme.classes(css);
            var baseTheme = coreTheme.classes(baseCss);
            if (prefix) {
                var prefixedCss = Object.keys(tslib_1.__assign({}, virtualTheme, variantTheme)).reduce(function (prefixCss, key) {
                    if (key.indexOf(prefix) === 0 && key !== prefix) {
                        var classKey = lowercaseFirstChar(key.replace(prefix, ''));
                        var variantClass = typeof variantTheme[key] === 'string' && variantTheme[key].trim();
                        var virtualClass = typeof virtualTheme[key] === 'string' && virtualTheme[key].trim();
                        if (!variantClass && virtualClass) {
                            prefixCss[classKey] = baseTheme[classKey] + " " + virtualClass;
                        }
                        if (variantClass) {
                            prefixCss[classKey] = variantTheme[key];
                        }
                    }
                    return prefixCss;
                }, {});
                baseTheme = tslib_1.__assign({}, baseTheme, prefixedCss);
                if (isThemeWithVariant(theme)) {
                    return {
                        theme: {
                            theme: tslib_1.__assign({}, theme.theme.theme, (_b = {}, _b[baseKey] = baseTheme, _b)),
                            variants: theme.theme.variants
                        },
                        variant: theme.variant
                    };
                }
                return tslib_1.__assign({}, theme, (_c = {}, _c[baseKey] = baseTheme, _c));
            }
            var constructedTheme = Object.keys(baseTheme).reduce(function (theme, key) {
                if (key === exports.THEME_KEY) {
                    return theme;
                }
                var variantComposesClass = typeof variantTheme[key] === 'string' && variantTheme[key].trim();
                var virtualClass = typeof virtualTheme[key] === 'string' && virtualTheme[key].trim();
                if (variantComposesClass) {
                    theme[key] = variantComposesClass;
                }
                else if (virtualClass) {
                    theme[key] = theme[key] + " " + virtualClass;
                }
                return theme;
            }, tslib_1.__assign({}, baseTheme));
            if (isThemeWithVariant(theme)) {
                return {
                    theme: {
                        theme: tslib_1.__assign({}, theme.theme.theme, (_d = {}, _d[baseKey] = constructedTheme, _d)),
                        variants: theme.theme.variants
                    },
                    variant: theme.variant
                };
            }
            return tslib_1.__assign({}, theme, (_e = {}, _e[baseKey] = constructedTheme, _e));
        },
        variant: function () {
            return properties().variant === 'inherit' ? undefined : coreVariant();
        },
        get: get,
        set: set,
        classes: classes
    };
});
exports.default = exports.theme;

/*# sourceMappingURL=theme.js.map*/