"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Provider = exports.ForwardContext = exports.useAllContexts = void 0;
const react_1 = __importStar(require("react"));
const ContextState_1 = require("../../hooks/ContextState");
const react_2 = require("@based/react");
const Toast_1 = require("../Toast/");
const AuthProvider_1 = require("../Auth/AuthProvider");
const kabouter_1 = require("kabouter");
const varsUtilities_1 = require("../../varsUtilities");
const useTheme_1 = require("../../hooks/useTheme");
const useAllContexts = () => {
    const state = (0, react_1.useContext)(ContextState_1.StateContext);
    const client = (0, react_2.useClient)();
    const router = (0, react_1.useContext)(kabouter_1.RouterContext);
    return { state, client, router };
};
exports.useAllContexts = useAllContexts;
const ForwardContext = ({ context, children }) => {
    let r = children;
    const { state, client, router } = (0, exports.useAllContexts)();
    if (state !== context.state) {
        r = react_1.default.createElement(ContextState_1.StateContext.Provider, { value: context.state }, r);
    }
    if (client !== context.client) {
        r = react_1.default.createElement(react_2.Provider, { client: context.client }, r);
    }
    if (router !== context.router) {
        r = (react_1.default.createElement(kabouter_1.RouterContext.Provider, { value: context.router }, r));
    }
    return react_1.default.createElement(react_1.default.Fragment, null, r);
};
exports.ForwardContext = ForwardContext;
// TODO: types!
const mergeNested = (theme, overwrite, key) => {
    if (overwrite[key]) {
        if (theme[key]) {
            for (const i in overwrite[key]) {
                if (theme[key][i]) {
                    for (const j in overwrite[key][i]) {
                        theme[key][i][j] = overwrite[key][i][j];
                    }
                }
                else {
                    theme[key][i] = overwrite[key][i];
                }
            }
        }
        else {
            theme[key] = overwrite[key];
        }
    }
};
const merge = (theme, overwrite) => {
    if (overwrite) {
        mergeNested(theme, overwrite, 'colors');
        mergeNested(theme, overwrite, 'light');
    }
};
const Provider = ({ children, style, path, client, themes, theme, fill, }) => {
    // theme logic
    //   useEffect(() => {
    //     if (themes) {
    //       const { base, dark } = themes
    //       merge(baseTheme, base)
    //       merge(darkTheme, dark)
    //       updateTheme()
    //     }
    //   }, [themes])
    //   useEffect(() => {
    //     if (theme) {
    //       updateTheme(theme === 'dark' ? darkTheme : baseTheme)
    //     }
    //   }, [theme])
    return (react_1.default.createElement("div", { style: {
            backgroundColor: (0, varsUtilities_1.color)('background', 'default', 'surface'),
            color: (0, varsUtilities_1.color)('content', 'default', 'primary'),
            height: fill ? '100vh' : null,
            width: '100%',
            display: 'flex',
            flexDirection: 'column',
            ...style,
        } },
        react_1.default.createElement(react_2.Provider, { client: client },
            react_1.default.createElement(kabouter_1.Router, { path: path || '' },
                react_1.default.createElement(Toast_1.ToastProvider, null,
                    react_1.default.createElement(AuthProvider_1.AuthProvider, null,
                        react_1.default.createElement(useTheme_1.ThemeProvider, null, children)))))));
};
exports.Provider = Provider;
//# sourceMappingURL=index.js.map