"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/projects/file"));
const API = __importStar(require("@/apis/project"));
const index_1 = require("@/constants/index");
const index_2 = require("@/foxI18n/index");
const index_3 = require("@/store/index");
function* handleFetchList(action) {
    yield (0, effects_1.put)(ACTIONS.updateLoading(true));
    const { params } = action.payload;
    const res = yield (0, effects_1.call)(API.getProjectFiles, params);
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushFileList(res.data.files, res.pageInfo));
    }
    else {
        const { global: { fetchListFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchListFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateLoading(false));
}
function* handleSave(action) {
    yield (0, effects_1.put)(ACTIONS.updateSaveLoading(true));
    const { params, cb } = action.payload;
    const { folderId, applicationId } = params;
    const { editFile } = index_3.store.getState().projects.file;
    const api = editFile.type === index_1.FileType.page
        ? editFile.id
            ? API.updatePage
            : API.addPage
        : editFile.id
            ? API.updateTemplate
            : API.addTemplate;
    const res = yield (0, effects_1.call)(api, {
        id: editFile.id,
        name: editFile.name,
        folderId,
        applicationId,
        tags: editFile.tags,
        suffix: editFile.suffix || index_1.defaultSuffix[editFile.type],
    });
    if (res.code === 200) {
        if (typeof cb === 'function')
            cb();
    }
    else {
        const { global: { saveFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || saveFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateSaveLoading(false));
}
function* handleDelete(action) {
    const { id, applicationId, folderId } = action.payload;
    const { pageInfo } = index_3.store.getState().projects.file;
    const res = yield (0, effects_1.call)(API.deleteFile, {
        id,
        applicationId,
        status: true,
    });
    const { global: { deleteSuccess, deleteFailed }, } = (0, index_2.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(deleteSuccess);
        // refresh list
        yield (0, effects_1.put)(ACTIONS.fetchFileList(Object.assign(Object.assign({}, pageInfo), { search: '', id: folderId, applicationId })));
    }
    else {
        antd_1.message.error(res.msg || deleteFailed);
    }
}
function* handleFetchParentFiles(action) {
    var _a;
    const { params, cb } = action.payload;
    const res = yield (0, effects_1.call)(API.fetchParentFiles, params);
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushParentFiles(res.data));
        if (typeof cb === 'function')
            cb((_a = res.data) === null || _a === void 0 ? void 0 : _a[0]);
    }
    else {
        const { global: { fetchListFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchListFailed);
    }
}
function* watch() {
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchFileList), handleFetchList);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.saveFile), handleSave);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.deleteFile), handleDelete);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchParentFiles), handleFetchParentFiles);
}
function* rootSaga() {
    yield (0, effects_1.all)([watch()]);
}
exports.default = rootSaga;
