"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/applications/detail/projects/file"));
const AUTH_API = __importStar(require("@/apis/authorize"));
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 { folderId, applicationId } = action.payload;
    const { editFile, pageInfo } = index_3.store.getState().applications.detail.projects.file;
    const api = editFile.type === index_1.FileType.page
        ? editFile.id
            ? API.updatePage
            : API.addPage
        : editFile.id
            ? API.updateTemplate
            : API.addTemplate;
    const rs = 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 (rs.code === 200) {
        yield (0, effects_1.put)(ACTIONS.openEditDrawer(false));
        yield (0, effects_1.put)(ACTIONS.fetchFileList(Object.assign(Object.assign({}, pageInfo), { search: '', id: folderId, applicationId })));
    }
    else {
        const { global: { fetchListFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(rs.msg || fetchListFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateSaveLoading(false));
}
function* handleDeleteFile(action) {
    const { id, applicationId, folderId } = action.payload;
    const rs = yield (0, effects_1.call)(API.deleteFile, {
        id,
        applicationId,
        status: true,
    });
    const { global: { deleteSuccess, deleteFailed }, } = (0, index_2.getBusinessI18n)();
    if (rs.code === 200) {
        antd_1.message.success(deleteSuccess);
        const { pageInfo } = index_3.store.getState().applications.detail.projects.file;
        yield (0, effects_1.put)(ACTIONS.fetchFileList(Object.assign(Object.assign({}, pageInfo), { search: '', id: folderId, applicationId })));
    }
    else {
        antd_1.message.error(rs.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* handleFetchAuthList(action) {
    yield (0, effects_1.put)(ACTIONS.updateAuthListLoading(true));
    const { params } = action.payload;
    const rs = yield (0, effects_1.call)(AUTH_API.authorizeFetch, params);
    if (rs.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushAuthList(rs.data || []));
    }
    else {
        const { global: { fetchListFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(rs.msg || fetchListFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateAuthListLoading(false));
}
function* handleSaveAuth(action) {
    const { params, cb } = action.payload;
    const rs = yield (0, effects_1.call)(AUTH_API.authorizeAdd, params);
    if (rs.code === 200) {
        if (typeof cb === 'function') {
            cb();
        }
    }
    else {
        const { global: { addFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(rs.msg || addFailed);
    }
}
function* handleDeleteAuth(action) {
    const { params, cb } = action.payload;
    const rs = yield (0, effects_1.call)(AUTH_API.authorizeDelete, params);
    const { global: { deleteSuccess, deleteFailed }, } = (0, index_2.getBusinessI18n)();
    if (rs.code === 200) {
        antd_1.message.success(deleteSuccess);
        if (typeof cb === 'function') {
            cb();
        }
    }
    else {
        antd_1.message.error(rs.msg || deleteFailed);
    }
}
function* handleFetchAuthUserList(action) {
    const { params } = action.payload;
    const rs = yield (0, effects_1.call)(AUTH_API.authorizeUserFetch, params);
    if (rs.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushUserList(rs.data || []));
    }
    else {
        const { global: { fetchListFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(rs.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), handleDeleteFile);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchParentFiles), handleFetchParentFiles);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchAuthList), handleFetchAuthList);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchUserList), handleFetchAuthUserList);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.saveAuthUser), handleSaveAuth);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.deleteAuthUser), handleDeleteAuth);
}
function* rootSaga() {
    yield (0, effects_1.all)([watch()]);
}
exports.default = rootSaga;
