import {
    type ReactNode
} from "react";
import {
    initializeInstances
} from "./hooks";
import type {
    NCoreUIKitConfig
} from "../types";
import {
    SafeAreaProvider
} from "react-native-safe-area-context";
import CoreContext from "../context";
import packageJSON from "../../package.json";

export const getNCoreUIKitVersion = () => packageJSON.version;

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 <SafeAreaProvider>
            <NCoreUIKitContext.Provider>
                {children}
            </NCoreUIKitContext.Provider>
        </SafeAreaProvider>;
    };
};

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;
    }
};
