import {
    type ReactNode
} from "react";
import {
    initializeInstances
} from "./hooks";
import type {
    NCoreUIKitConfig
} from "../types";
import CoreContext from "../context";

export class NCoreUIKitBase<T extends NCoreUIKitConfig> {
    NCoreUIKitContext: CoreContext<T>;

    constructor(initialState: T) {
        this.NCoreUIKitContext = new CoreContext(initialState);
    }

    Provider = ({
        children
    }: {
        children: ReactNode;
    }) => {
        const NCoreUIKitContext = this.NCoreUIKitContext;

        return <NCoreUIKitContext.Provider>
            {children}
        </NCoreUIKitContext.Provider>;
    };
};

let NCoreUIKit!: NCoreUIKitBase<NCoreUIKitConfig>;

export const setupNCoreUIKit = (initialState: NCoreUIKitConfig, isCustom: boolean = false) => {
    if(isCustom) {
        return new NCoreUIKitBase(initialState);
    } else {
        NCoreUIKit = new NCoreUIKitBase(initialState);

        initializeInstances(NCoreUIKit);

        return NCoreUIKit;
    }
};
