"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
const antd_1 = require("antd");
const axios_1 = __importDefault(require("axios"));
const foxpage_js_sdk_1 = require("@foxpage/foxpage-js-sdk");
const index_1 = require("@/foxI18n/index");
const parsePageInServer = (page, opt, host) => __awaiter(void 0, void 0, void 0, function* () {
    const url = `${foxpage_js_sdk_1.PARSE_PAGE_PATH}?&host=${host}`;
    const result = yield axios_1.default.post(url, {
        page,
        opt: {
            appId: opt.appInfo.appId,
            relationInfo: opt.relationInfo,
            locale: opt.locale,
        },
    });
    return result;
});
/**
 * parse page content
 * @param data page content
 * @param opt parse options
 * @returns parsed
 */
const parse = (data, opt) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    const { content, relations = {} } = data;
    const { application, locale } = opt;
    const appInfo = {
        appId: application.id,
        slug: application.slug,
        configs: {},
    };
    const relationInfo = {
        templates: relations.templates || [],
        variables: relations.variables || [],
        conditions: relations.conditions || [],
        functions: relations.functions || [], //FPFunction[]
    };
    try {
        // @ts-ignore
        if (APP_CONFIG.env === 'dev') {
            throw new Error('is develop');
        }
        const host = ((_b = (_a = application.host) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.url) || '';
        if (!host) {
            throw new Error('no host');
        }
        const result = (yield parsePageInServer(content, {
            appInfo,
            relationInfo: relationInfo,
            locale,
        }, `${host}/${application.slug}`));
        if (result.status === 200) {
            if (result.data.status) {
                const schemas = result.data.result.parsedPage;
                return Object.assign(Object.assign({}, data), { content: Object.assign(Object.assign({}, content), { schemas }) });
            }
            else {
                const { builder: { parsePageFailed }, } = (0, index_1.getBusinessI18n)();
                antd_1.message.error(parsePageFailed);
                console.error(result.data.result);
                return Object.assign(Object.assign({}, data), { content: Object.assign(Object.assign({}, content), { schemas: [] }) });
            }
        }
        return null;
    }
    catch (e) {
        // client(js) parse
        const result = yield (0, foxpage_js_sdk_1.parsePage)(content, {
            appInfo,
            relationInfo: relationInfo,
        });
        return Object.assign(Object.assign({}, data), { content: Object.assign(Object.assign({}, content), result.page) });
    }
});
exports.parse = parse;
