"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onEditorLoad = void 0;
const tslib_1 = require("tslib");
const app_context_1 = require("@builder.io/app-context");
const template_1 = require("lodash/template");
const capitalize_1 = require("lodash/capitalize");
const onEditorLoad = (config, apiOperations, resourceName) => ({ safeReaction, updatePreviewUrl }) => {
    // compile previewResource if any, to allow user to have templated editing urls for their models (e.g in cases of product page template)
    safeReaction(() => {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        const modelUrl = (_a = app_context_1.default.designerState.editingModel) === null || _a === void 0 ? void 0 : _a.examplePageUrl;
        if (!modelUrl) {
            return;
        }
        const previewField = (_c = (_b = app_context_1.default.designerState.editingModel) === null || _b === void 0 ? void 0 : _b.fields.find((field) => field.type === `${config.name}${(0, capitalize_1.default)(resourceName)}Preview`)) === null || _c === void 0 ? void 0 : _c.name;
        if (previewField && app_context_1.default.designerState.editingContentModel) {
            const resourceId = (_d = app_context_1.default.designerState.editingContentModel.data
                .get(previewField)) === null || _d === void 0 ? void 0 : _d.options.get(resourceName);
            return apiOperations[resourceName].findById(resourceId);
        }
        const pluginSettings = app_context_1.default.user.organization.value.settings.plugins.get(config.id);
        const shouldSync = pluginSettings.get('syncPreviewUrlWithTargeting');
        const query = (_f = (_e = app_context_1.default.designerState.editingContentModel) === null || _e === void 0 ? void 0 : _e.query) === null || _f === void 0 ? void 0 : _f.toJSON();
        const customAttributes = (_g = app_context_1.default.user.organization.value.customTargetingAttributes) === null || _g === void 0 ? void 0 : _g.toJSON();
        if (shouldSync && customAttributes && query) {
            const apiService = apiOperations[resourceName];
            for (const [key, value] of Object.entries(customAttributes)) {
                const targetingValue = (_h = query.find((q) => q.property === key)) === null || _h === void 0 ? void 0 : _h.value;
                if (targetingValue && value.type === `${config.name}${(0, capitalize_1.default)(resourceName)}`) {
                    return apiService.findById(targetingValue);
                }
                if (apiService.findByHandle &&
                    targetingValue &&
                    value.type === `${config.name}${(0, capitalize_1.default)(resourceName)}Handle`) {
                    return apiService.findByHandle(targetingValue);
                }
            }
        }
    }, (resourcePromise) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        var _a;
        const modelUrl = (_a = app_context_1.default.designerState.editingModel) === null || _a === void 0 ? void 0 : _a.examplePageUrl;
        if (resourcePromise && modelUrl) {
            const previewResource = yield resourcePromise;
            const compiled = (0, template_1.default)(modelUrl);
            const previewUrl = compiled(withDefaults({
                previewResource,
                [`preview${(0, capitalize_1.default)(resourceName)}`]: previewResource,
            }));
            if (modelUrl !== previewUrl) {
                updatePreviewUrl(previewUrl);
                app_context_1.default.snackBar.show(`Previewing ${previewUrl}`);
            }
        }
    }));
};
exports.onEditorLoad = onEditorLoad;
const withDefaults = (obj) => {
    var _a;
    return (Object.assign(Object.assign({}, obj), { space: {
            siteUrl: (_a = app_context_1.default.user.organization.value) === null || _a === void 0 ? void 0 : _a.siteUrl,
        } }));
};
//# sourceMappingURL=on-editor-load.js.map