import {
    type ReactNode
} from "react";
import NCoreUIKitLocalize from "./localize";
import NCoreUIKitSnackBar from "./snackBar";
import NCoreUIKitDialog from "./dialog";
import NCoreUIKitModal from "./modal";
import NCoreUIKitTheme from "./theme";
import NCoreUIKitToast from "./toast";
import {
    type NCoreUIKitConfig
} from "../types";
import {
    Host
} from "../helpers/portalize";

class CoreContext<T extends NCoreUIKitConfig> {
    NCoreUIKitLocalize: NCoreUIKitLocalize<T>;
    NCoreUIKitSnackBar: NCoreUIKitSnackBar;
    NCoreUIKitTheme: NCoreUIKitTheme<T>;
    NCoreUIKitDialog: NCoreUIKitDialog;
    NCoreUIKitModal: NCoreUIKitModal;
    NCoreUIKitToast: NCoreUIKitToast;

    constructor(initialState: T) {
        this.NCoreUIKitTheme = new NCoreUIKitTheme({
            initialSelectedActiveSharpness: initialState.initialSelectedActiveSharpness,
            initialSelectedGapPropagation: initialState.initialSelectedGapPropagation,
            initialSelectedPalette: initialState.initialSelectedPalette,
            initialSelectedTheme: initialState.initialSelectedTheme,
            projectThemes: initialState.projectThemes
        });

        this.NCoreUIKitLocalize = new NCoreUIKitLocalize({
            initialSelectedLocale: initialState.initialSelectedLocale,
            projectLocales: initialState.projectLocales
        });

        this.NCoreUIKitModal = new NCoreUIKitModal({
            data: []
        });

        this.NCoreUIKitToast = new NCoreUIKitToast({
            data: []
        });

        this.NCoreUIKitSnackBar = new NCoreUIKitSnackBar({
            data: []
        });

        this.NCoreUIKitDialog = new NCoreUIKitDialog({
            data: []
        });
    }

    Provider = ({
        children
    }: {
        children: ReactNode
    }) => {
        const LocalizeContext = this.NCoreUIKitLocalize;
        const SnackBarContext = this.NCoreUIKitSnackBar;
        const DialogContext = this.NCoreUIKitDialog;
        const ModalContext = this.NCoreUIKitModal;
        const ToastContext = this.NCoreUIKitToast;
        const ThemeContext = this.NCoreUIKitTheme;

        return <ThemeContext.Provider>
            <LocalizeContext.Provider>
                <Host name="snack-bar-system">
                    <SnackBarContext.Provider>
                        <SnackBarContext.Render>
                            <Host name="toast-system">
                                <Host name="modal-system">
                                    <ModalContext.Provider>
                                        <ModalContext.Render>
                                            <DialogContext.Provider>
                                                <DialogContext.Render>
                                                    <ToastContext.Provider>
                                                        <ToastContext.Render>
                                                            {children}
                                                        </ToastContext.Render>
                                                    </ToastContext.Provider>
                                                </DialogContext.Render>
                                            </DialogContext.Provider>
                                        </ModalContext.Render>
                                    </ModalContext.Provider>
                                </Host>
                            </Host>
                        </SnackBarContext.Render>
                    </SnackBarContext.Provider>
                </Host>
            </LocalizeContext.Provider>
        </ThemeContext.Provider>;
    };
};
export default CoreContext;
