import { nanoid } from "nanoid";
import React from "react";
import iconsList from "./components/iconsList";
import { RuntimeContext } from "./RuntimeContext";
import { componentAliases } from "./componentAliases";
export const ComponentsMap = {};
export const defineComponent = (component) => component;
export const registerComponent = (id, component) => {
    // @ts-ignore
    ComponentsMap[id] = Object.assign({ id, name: id }, component);
    return {
        id,
        // @ts-ignore
        component: ComponentsMap[id],
    };
};
export const unregisterComponent = (id) => {
    delete ComponentsMap[id];
};
// @ts-ignore
if (typeof window !== "undefined" && !window.qoreApps) {
    // @ts-ignore
    window.qoreApps = Object.assign(Object.assign({}, window.qoreApps), { registerComponent: (id, component) => ({ id, component: Object.assign({}, component) }), registerNpmComponent: (name, component) => ({ name, component }), defineComponent: (c) => c });
}
export const getComponent = (id) => {
    var _a;
    return (_a = ComponentsMap[id]) !== null && _a !== void 0 ? _a : ComponentsMap[componentAliases[id]];
};
export const getComponentID = (instance) => componentAliases[instance.componentID] &&
    instance.componentID === componentAliases[instance.componentID]
    ? componentAliases[instance.componentID]
    : instance.componentID;
export const isComponent = (instance, id) => getComponentID(instance) === id;
export const buildComponent = (id, config) => {
    const Comp = getComponent(id);
    return {
        componentID: id,
        kind: Comp.kind || "element",
        options: {},
        showCondition: { type: "all", conditions: [] },
        source: Comp.type === "list" || Comp.type === "standalone-list"
            ? Object.assign({ type: Comp.type, params: {}, sorts: { field: "", type: "asc" } }, (Comp.id === "List" || Comp.id === "List Card"
                ? {
                    limitType: "custom",
                }
                : {}))
            : Comp.type === "nested-list"
                ? {
                    fieldId: "",
                    type: "nested-list",
                    rowId: "",
                    params: {},
                }
                : Comp.type === "record"
                    ? {
                        type: "record",
                        target: undefined,
                        params: {},
                        rowId: "",
                        defaultRow: { type: "first-row" },
                    }
                    : Comp.type === "record-list"
                        ? {
                            type: "record-list",
                            target: undefined,
                            params: {},
                            sourceType: "record",
                        }
                        : undefined,
        id: nanoid(3),
        properties: Object.assign({}, Comp.defaultProps),
        metadata: Object.assign({}, config === null || config === void 0 ? void 0 : config.metadata),
    };
};
export const getComponentList = (filter) => {
    return Object.entries(ComponentsMap)
        .filter(filter || (([, component]) => !component.hidden))
        .map(([id, component]) => (Object.assign({ id }, component)));
};
export const componentGroups = [
    "text",
    "media",
    "list",
    "button",
    "input",
    "layout",
    "data",
    "advanced",
];
export const getGroupedComponentList = (list = getComponentList()) => {
    const groups = list.reduce((result, item) => {
        if (!result[item.group]) {
            result[item.group] = [];
        }
        result[item.group].push(item);
        return result;
    }, {});
    groups["advanced"] = [
        {
            id: "Custom Component",
            type: "record",
            icon: "Atom",
            group: "advanced",
            defaultProps: {},
            propDefinition: {},
            Component: () => null,
        },
    ];
    return groups;
};
export const convertSourceTo = {
    record: (source, rowId) => ({
        target: source.target,
        rowId,
        type: "record",
        params: {},
        defaultRow: { type: "first-row" },
    }),
    list: (source, params, sorts) => ({
        target: source.target,
        type: "list",
        sorts,
        params,
    }),
};
const pageFactory = (properties) => {
    return () => ({
        components: [],
        id: nanoid(3),
        properties,
        title: "Untitled page",
        defaultRow: {
            type: "row-id",
            rowId: "",
        },
        staticId: false,
        navbar: {
            bottom: {
                show: true,
            },
            sidetop: {
                show: true,
            },
            drawer: {
                show: true,
            },
        },
    });
};
export const pageDefinition = {
    title: { type: "string", options: { format: "text" } },
    onOpen: { type: "action", options: [{ type: "none" }] },
    onScrollToBottom: { type: "action", options: [{ type: "none" }] },
    headerStyle: {
        type: "string",
        options: {
            format: "select",
            options: [
                { label: "None", value: "none" },
                { label: "General", value: "default" },
            ],
        },
    },
    action: { type: "action", options: [{ type: "none" }] },
    icon: {
        type: "string",
        options: {
            format: "icon",
            options: [
                ...Object.keys(iconsList).map((item) => ({
                    label: item,
                    value: item,
                })),
                {
                    label: "None",
                    value: "none",
                },
            ],
        },
    },
    refreshInterval: { type: "number", options: { unit: "seconds" } },
    onSuccess: { type: "action", options: [{ type: "none" }] },
};
export const pagePropertyDefinition = {
    title: { type: "string", options: { format: "text" } },
    onOpen: { type: "action", options: [{ type: "none" }] },
    onScrollToBottom: { type: "action", options: [{ type: "none" }] },
    headerStyle: {
        type: "string",
        options: {
            format: "select",
            options: [
                { label: "None", value: "none" },
                { label: "General", value: "default" },
            ],
        },
    },
};
export const integrations = {};
export function registerIntegration(integration, builder) {
    integrations[integration.name] = integration;
    const useIntegration = () => {
        const integrationConfig = RuntimeContext.useSelectState((state) => { var _a; return (_a = state.integrations) === null || _a === void 0 ? void 0 : _a[integration.name]; });
        const config = React.useMemo(() => (integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.properties.values) ||
            integration.properties.values, [integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.properties.values, integration.properties.values]);
        const integrationObject = React.useMemo(() => {
            return builder(config);
        }, [config, builder]);
        const run = React.useCallback((runner) => {
            integrationConfig && runner(integrationObject);
        }, [integrationObject, integrationConfig]);
        return { run };
    };
    return {
        useIntegration,
    };
}
//# sourceMappingURL=interfaces.js.map