import { __awaiter } from "tslib";
// @ts-nocheck
import React from "react";
import { componentAliases } from "./componentAliases";
import { registerComponent, getComponent, } from "./interfaces";
import { RuntimeContext } from "./RuntimeContext";
import { importNpmComponent } from "./utils/importComponent";
import { mutate } from "swr";
import useSWRImmutable from "swr/immutable";
const builtinComponents = {
    "@qorebase/action-text-component": () => import("@qorebase/action-text-component"),
    "@qorebase/banner-component": () => import("@qorebase/banner-component"),
    "@qorebase/bar-chart-component": () => import("@qorebase/bar-chart-component"),
    "@qorebase/barcode-component": () => import("@qorebase/barcode-component"),
    "@qorebase/barcode-generator-component": () => import("@qorebase/barcode-generator-component"),
    "@qorebase/button-component": () => import("@qorebase/button-component"),
    "@qorebase/button-bar-component": () => import("@qorebase/button-bar-component"),
    "@qorebase/card-component": () => import("@qorebase/card-component"),
    "@qorebase/carousel-component": () => import("@qorebase/carousel-component"),
    "@qorebase/container-component": () => import("@qorebase/container-component"),
    "@qorebase/date-picker-component": () => import("@qorebase/date-picker-component"),
    "@qorebase/description-text-component": () => import("@qorebase/description-text-component"),
    "@qorebase/divider-component": () => import("@qorebase/divider-component"),
    "@qorebase/dropdown-component": () => import("@qorebase/dropdown-component"),
    "@qorebase/dropdown-list-component": () => import("@qorebase/dropdown-list-component"),
    "@qorebase/dropdown-relation-component": () => import("@qorebase/dropdown-relation-component"),
    "@qorebase/embed-component": () => import("@qorebase/embed-component"),
    "@qorebase/file-picker-component": () => import("@qorebase/file-picker-component"),
    "@qorebase/floating-button-component": () => import("@qorebase/floating-button-component"),
    "@qorebase/headline-component": () => import("@qorebase/headline-component"),
    "@qorebase/hint-component": () => import("@qorebase/hint-component"),
    "@qorebase/horizontal-tile-component": () => import("@qorebase/horizontal-tile-component"),
    "@qorebase/icon-button-component": () => import("@qorebase/icon-button-component"),
    "@qorebase/image-component": () => import("@qorebase/image-component"),
    "@qorebase/line-chart-component": () => import("@qorebase/line-chart-component"),
    "@qorebase/list-component": () => import("@qorebase/list-component"),
    "@qorebase/list-card-component": () => import("@qorebase/list-card-component"),
    "@qorebase/location-component": () => import("@qorebase/location-component"),
    "@qorebase/location-input-component": () => import("@qorebase/location-input-component"),
    "@qorebase/multiple-file-picker-component": () => import("@qorebase/multiple-file-picker-component"),
    "@qorebase/number-input-component": () => import("@qorebase/number-input-component"),
    // "@qorebase/paragraph-component": () =>
    //   import("@qorebase/paragraph-component"),
    "@qorebase/pie-chart-component": () => import("@qorebase/pie-chart-component"),
    "@qorebase/qr-code-generator-component": () => import("@qorebase/qr-code-generator-component"),
    "@qorebase/qr-code-scanner-component": () => import("@qorebase/qr-code-scanner-component"),
    "@qorebase/rating-component": () => import("@qorebase/rating-component"),
    "@qorebase/repeatable-container-component": () => import("@qorebase/repeatable-container-component"),
    "@qorebase/rich-text-component": () => import("@qorebase/rich-text-component"),
    // "@qorebase/rich-text-input-component": () =>
    //   import("@qorebase/rich-text-input-component"),
    "@qorebase/scorecard-component": () => import("@qorebase/scorecard-component"),
    "@qorebase/sticky-container-component": () => import("@qorebase/sticky-container-component"),
    "@qorebase/table-component": () => import("@qorebase/table-component"),
    "@qorebase/text-component": () => import("@qorebase/text-component"),
    "@qorebase/text-area-component": () => import("@qorebase/text-area-component"),
    "@qorebase/text-input-component": () => import("@qorebase/text-input-component"),
    "@qorebase/toggle-component": () => import("@qorebase/toggle-component"),
    "@qorebase/vertical-tile-component": () => import("@qorebase/vertical-tile-component"),
};
export const useImportComponentLazy = () => {
    const npmComponents = RuntimeContext.useSelectState((state) => state.components);
    return React.useCallback((name) => __awaiter(void 0, void 0, void 0, function* () {
        var _a;
        const registeredComponent = getComponent(name);
        if (registeredComponent) {
            return registeredComponent;
        }
        const npmComponent = npmComponents.find((component) => component.name === name && component.enabled);
        if (npmComponent) {
            return (yield importNpmComponent(name, npmComponent.version)).component;
        }
        const importBuiltinComponent = (_a = builtinComponents[name]) !== null && _a !== void 0 ? _a : builtinComponents[componentAliases[name]];
        if (!importBuiltinComponent) {
            throw Error(`Component ${name} not found`);
        }
        return importBuiltinComponent().then((module) => registerComponent(name, module.default).component);
    }), [npmComponents]);
};
export const useImportComponent = (name) => {
    const importComponent = useImportComponentLazy();
    const { data } = useSWRImmutable(["npm components", name], (_, name) => importComponent(name), {
        isPaused: () => !name,
    });
    return data;
};
export const importBuiltins = (ignores) => __awaiter(void 0, void 0, void 0, function* () {
    return Promise.all(Object.entries(builtinComponents)
        .filter(([name]) => !ignores.includes(name))
        .map(([name, importComponent]) => importComponent().then((module) => {
        mutate(["npm components", name], module.default);
        return registerComponent(name, module.default);
    })));
});
//# sourceMappingURL=builtinComponents.js.map