import { lazy } from "react";
import { MainStateManager } from "./MainStateManager";
import { IMainStateFactory } from "./Types";

export type IPageLoadByKey = {
    pageKey: string,
    Page: React.ComponentType<any>,
    ExtraPageToolStripItems?: React.ComponentType<{ mainState: any; }>;
};

export class PageLoadByKey {
    constructor(
        public mainStateManager: MainStateManager,
    ) { }

    public pages: IPageLoadByKey[] = [];

    public add = (key: string, Page: React.ComponentType<{ mainState: IMainStateFactory }>, ExtraPageToolStripItems?: React.ComponentType<{ mainState: any; }>) => {
        const find = this.pages.find(i => i.pageKey === key);
        if (find) {
            find.Page = Page;
            find.ExtraPageToolStripItems = ExtraPageToolStripItems;
        } else {
            this.pages.push({ pageKey: key, Page, ExtraPageToolStripItems });
        }
    }

    public get = (key: string): IPageLoadByKey => {
        const find = this.pages.find(i => i.pageKey.toUpperCase() === key.toUpperCase());
        if (find) {
            return find;
        } else {
            console.error(`can not find any page key : ${key}`);
            throw Error(`can not find any page key : ${key}`);
        }
    }

    public isValid = (key: string): boolean => {
        const find = this.pages.find(i => i.pageKey === key);
        if (find) {
            return true;
        } else {
            return false;
        }
    }
}