"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerDataPlugin = void 0;
const tslib_1 = require("tslib");
const sdk_1 = require("@builder.io/sdk");
const app_context_1 = require("@builder.io/app-context");
const registerDataPlugin = (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 savedSettings = app_context_1.default.user.organization.value.settings.plugins.get(config.id);
        const allSettings = Object.assign(Object.assign({}, config), (yield apiOperationsFromSettings(savedSettings)));
        app_context_1.default.registerDataPlugin(allSettings);
    });
    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), { isDataPlugin: 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.registerDataPlugin = registerDataPlugin;
//# sourceMappingURL=data.js.map