"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const antd_1 = require("antd");
const effects_1 = require("redux-saga/effects");
const typesafe_actions_1 = require("typesafe-actions");
const ACTIONS = __importStar(require("@/actions/builder/header"));
const API = __importStar(require("@/apis/builder"));
const store_1 = require("@/apis/store");
const index_1 = require("@/constants/index");
const index_2 = require("@/foxI18n/index");
const index_3 = require("@/store/index");
function* handleFetchCatalog(action) {
    yield (0, effects_1.put)(ACTIONS.updateLoading(true));
    const { applicationId, folderId = '' } = action.payload;
    const res = yield (0, effects_1.call)(API.fetchCatalog, {
        applicationId,
        id: folderId,
    });
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushCatalog(res.data));
    }
    else {
        const { builder: { fetchCatalogFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchCatalogFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateLoading(false));
}
function* handleSelectContent(action) {
    const { applicationId, folderId, fileId, contentId, locale, fileType, } = action.payload;
    yield (0, effects_1.all)([
        (0, effects_1.put)(ACTIONS.updateLocale(locale || '')),
        (0, effects_1.put)(ACTIONS.updateContentInfo({ applicationId, contentId, fileId, folderId, fileType })),
    ]);
}
// dls & mock related
function* handleFetchDsl(action) {
    var _a, _b;
    yield (0, effects_1.put)(ACTIONS.updateDSLLoading(true));
    const { fileType } = index_3.store.getState().builder.header;
    const { applicationId, ids } = action.payload;
    const res = yield (0, effects_1.call)(fileType === index_1.FileType.page ? API.fetchPageDsl : API.fetchTemplateDsl, {
        applicationId,
        ids,
    });
    if (res.code === 200 && ((_a = res.data) === null || _a === void 0 ? void 0 : _a.length) > 0) {
        yield (0, effects_1.put)(ACTIONS.pushDsl((_b = res.data[0]) === null || _b === void 0 ? void 0 : _b.content));
    }
    else {
        const { builder: { fetchDslFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchDslFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateDSLLoading(false));
}
function* handleSaveMock(action) {
    var _a, _b, _c;
    yield (0, effects_1.put)(ACTIONS.updateMockLoading(true));
    // call mock add api
    const { params, cb } = action.payload;
    const { applicationId, content } = params;
    // get specific api & params with different update type
    const mockId = (_a = params === null || params === void 0 ? void 0 : params.content) === null || _a === void 0 ? void 0 : _a.id;
    const api = !!mockId ? API.updateMock : API.saveMock;
    const newParams = !mockId
        ? params
        : {
            applicationId,
            id: mockId,
            content,
        };
    const res = yield (0, effects_1.call)(api, newParams);
    if (res.code === 200) {
        // record new mock id
        const newMockId = (_b = res.data) === null || _b === void 0 ? void 0 : _b.contentId;
        if (newMockId) {
            yield (0, effects_1.put)(ACTIONS.updateMockId(newMockId));
        }
        const newVersionId = (_c = res.data) === null || _c === void 0 ? void 0 : _c.id;
        if (typeof cb === 'function')
            cb(newVersionId);
    }
    else {
        const { global: { addFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || addFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateMockLoading(false));
}
function* handlePublishMock(action) {
    yield (0, effects_1.put)(ACTIONS.updateMockLoading(true));
    const { params, cb } = action.payload;
    const res = yield (0, effects_1.call)(API.publishMock, params);
    const { global: { publishSuccess, publishFailed }, } = (0, index_2.getBusinessI18n)();
    if (res.code === 200) {
        if (typeof cb === 'function') {
            cb();
        }
        antd_1.message.success(publishSuccess);
    }
    else {
        antd_1.message.error(res.msg || publishFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateMockLoading(false));
}
function* handleFetchApplicationTemplate(action) {
    yield (0, effects_1.put)(ACTIONS.updateStoreLoading(true));
    const { applicationId } = action.payload;
    const res = yield (0, effects_1.call)(API.fetchTemplates, {
        applicationId,
    });
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushApplicationTemplates(res.data));
    }
    else {
        const { builder: { fetchTemplateFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchTemplateFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateStoreLoading(false));
}
function* handleFetchStoreProjectGoods(action) {
    yield (0, effects_1.put)(ACTIONS.updateStoreLoading(true));
    const { applicationId, type, page, size } = action.payload;
    const res = yield (0, effects_1.call)(store_1.fetchApplicationStoreProjectGoods, {
        applicationId,
        type,
        page,
        size,
    });
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushStoreProjectGoods(res.data, res.pageInfo));
    }
    else {
        const { builder: { fetchTemplateFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchTemplateFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateStoreLoading(false));
}
function* watch() {
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchCatalog), handleFetchCatalog);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.selectContent), handleSelectContent);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchDsl), handleFetchDsl);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.saveMock), handleSaveMock);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.publishMock), handlePublishMock);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchApplicationTemplate), handleFetchApplicationTemplate);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchStoreProjectGoods), handleFetchStoreProjectGoods);
}
function* rootSaga() {
    yield (0, effects_1.all)([watch()]);
}
exports.default = rootSaga;
