"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRelation = void 0;
const antd_1 = require("antd");
const application_1 = require("@/apis/application");
const index_1 = require("@/foxI18n/index");
const index_2 = require("@/store/index");
const utils_1 = require("../utils");
function getRelation(content, relations) {
    return __awaiter(this, void 0, void 0, function* () {
        let serverVariables = [];
        // init & check relation (current only variables)
        const { relation, invalids } = (0, utils_1.initRelation)(content, relations);
        const realInvalids = [];
        if (invalids.length > 0) {
            const { application, file } = index_2.store.getState().builder.main;
            const map = {};
            const invalidNames = invalids.map((item) => {
                const name = item.split(':')[0];
                map[item] = name;
                return name;
            });
            const result = yield (0, application_1.searchVariable)({
                applicationId: application === null || application === void 0 ? void 0 : application.id,
                id: file.folderId,
                names: invalidNames,
                size: 100,
            });
            // init invalids
            if (result.code === 200) {
                serverVariables = result.data || [];
                invalids.forEach((item) => {
                    const initd = (0, utils_1.initVariableRelation)(map[item], result.data);
                    if (initd) {
                        relation[item] = initd;
                    }
                    else {
                        realInvalids.push(map[item]);
                    }
                });
            }
            else {
                const { global: { fetchListFailed }, } = (0, index_1.getBusinessI18n)();
                antd_1.message.error(fetchListFailed);
            }
            // still invalids
            if (realInvalids.length > 0) {
                const { builder: { relationMatchError }, } = (0, index_1.getBusinessI18n)();
                antd_1.message.error(`[ ${realInvalids.toString()} ] ${relationMatchError}`);
            }
        }
        return { relation, realInvalids, data: { variables: serverVariables } };
    });
}
exports.getRelation = getRelation;
