"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResourcePreviewCell = exports.registerCommercePlugin = void 0;
const tslib_1 = require("tslib");
const sdk_1 = require("@builder.io/sdk");
const ResourcesPicker_1 = require("./editors/ResourcesPicker");
const ResourcesList_1 = require("./editors/ResourcesList");
const app_context_1 = require("@builder.io/app-context");
const react_1 = require("react");
const on_editor_load_1 = require("./actions/on-editor-load");
const error_boundary_1 = require("./components/error-boundary");
const capitalize_1 = require("lodash/capitalize");
const pluralize_1 = require("pluralize");
const registerCommercePlugin = (config, apiOperationsFromSettings) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    const registerEditors = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        const apiOperations = yield apiOperationsFromSettings(app_context_1.default.user.organization.value.settings.plugins.get(config.id));
        const resources = Object.keys(apiOperations);
        resources.forEach(resourceName => {
            const contextProps = {
                resourceName,
                pluginId: config.id,
                pluginName: config.name,
                api: apiOperations,
            };
            sdk_1.Builder.register('editor.onLoad', (0, on_editor_load_1.onEditorLoad)(config, apiOperations, resourceName));
            const resourcePicker = apiOperations[resourceName].resourcePicker;
            sdk_1.Builder.registerEditor({
                name: `${config.name}${(0, capitalize_1.default)(resourceName)}`,
                isDataResource: true,
                pluginId: config.id,
                component: (props) => (react_1.default.createElement(error_boundary_1.ErrorBoundary, null,
                    react_1.default.createElement(ResourcesPicker_1.ResourcesPickerButton, Object.assign({ resourcePicker: resourcePicker }, props, contextProps)))),
            });
            if (!config.noPreviewTypes) {
                sdk_1.Builder.registerEditor({
                    name: `${config.name}${(0, capitalize_1.default)(resourceName)}Preview`,
                    component: (props) => (react_1.default.createElement(error_boundary_1.ErrorBoundary, null,
                        react_1.default.createElement(ResourcesPicker_1.ResourcesPickerButton, Object.assign({ resourcePicker: resourcePicker }, props, contextProps, { previewType: `${config.name}${(0, capitalize_1.default)(resourceName)}Preview`, isPreview: true })))),
                });
            }
            if (apiOperations[resourceName].findByHandle) {
                sdk_1.Builder.registerEditor({
                    name: `${config.name}${(0, capitalize_1.default)(resourceName)}Handle`,
                    component: (props) => (react_1.default.createElement(error_boundary_1.ErrorBoundary, null,
                        react_1.default.createElement(ResourcesPicker_1.ResourcesPickerButton, Object.assign({ resourcePicker: resourcePicker }, props, contextProps, { handleOnly: true })))),
                });
            }
            sdk_1.Builder.registerEditor({
                name: `${config.name}${(0, capitalize_1.default)(pluralize_1.default.plural(resourceName))}List`,
                enum: true,
                component: (props) => (react_1.default.createElement(error_boundary_1.ErrorBoundary, null,
                    react_1.default.createElement(ResourcesList_1.PickResourcesListButton, Object.assign({ resourcePicker: resourcePicker }, props, contextProps)))),
            });
        });
    });
    const onSave = config.onSave;
    config.onSave = (actions) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        yield actions.updateSettings({ hasConnected: true });
        if (onSave) {
            yield onSave(actions);
        }
        registerEditors();
    });
    sdk_1.Builder.register('plugin', Object.assign(Object.assign({}, config), { isSourcePlugin: true }));
    sdk_1.Builder.register('app.onLoad', (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ triggerSettingsDialog }) {
        const pluginSettings = app_context_1.default.user.organization.value.settings.plugins.get(config.id);
        const hasConnected = pluginSettings === null || pluginSettings === void 0 ? void 0 : pluginSettings.get('hasConnected');
        if (!hasConnected) {
            yield triggerSettingsDialog(config.id);
        }
    }));
    const savedSettings = app_context_1.default.user.organization.value.settings.plugins.get(config.id);
    if (!savedSettings) {
        return;
    }
    yield registerEditors();
});
exports.registerCommercePlugin = registerCommercePlugin;
var ResourcesPicker_2 = require("./editors/ResourcesPicker");
Object.defineProperty(exports, "ResourcePreviewCell", { enumerable: true, get: function () { return ResourcesPicker_2.ResourcePreviewCell; } });
//# sourceMappingURL=commerce.js.map