"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/resources/detail"));
const API = __importStar(require("@/apis/application"));
const index_1 = require("@/foxI18n/index");
const index_2 = require("@/store/index");
function* addResourceFile(action) {
    const { params, options } = action.payload;
    const { applicationId, curFolderId, filepath } = params || {};
    const rs = yield (0, effects_1.call)(API.postResourcesContents, {
        applicationId,
        folderId: curFolderId,
        content: {
            realPath: filepath,
        },
    });
    const { global: { saveSuccess, saveFailed }, } = (0, index_1.getBusinessI18n)();
    if (rs.code === 200) {
        antd_1.message.success(saveSuccess);
        yield (0, effects_1.put)(ACTIONS.fetchResourcesList({ applicationId }));
        const { onSuccess } = options || {};
        if (typeof onSuccess === 'function')
            onSuccess();
    }
    else {
        antd_1.message.error(rs.msg || saveFailed);
    }
}
function* updateResourceFile(action) {
    const { params, options } = action.payload;
    const { applicationId, fileId, filepath } = params || {};
    const res = yield (0, effects_1.call)(API.putResourcesContents, {
        applicationId,
        id: fileId,
        content: {
            realPath: filepath,
        },
    });
    const { global: { updateSuccess, updateFailed }, } = (0, index_1.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(updateSuccess);
        yield (0, effects_1.put)(ACTIONS.fetchResourcesList({ applicationId }));
        const { onSuccess } = options || {};
        if (typeof onSuccess === 'function')
            onSuccess();
    }
    else {
        antd_1.message.error(res.msg || updateFailed);
    }
}
function* addResourceFolder(action) {
    const { params, options } = action.payload;
    const { applicationId, curFolderId, name } = params || {};
    const res = yield (0, effects_1.call)(API.postResourcesFolders, {
        applicationId,
        parentFolderId: curFolderId,
        name,
    });
    const { global: { saveSuccess, saveFailed }, } = (0, index_1.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(saveSuccess);
        yield (0, effects_1.put)(ACTIONS.fetchResourcesList({ applicationId }));
        const { onSuccess } = options || {};
        if (typeof onSuccess === 'function')
            onSuccess();
    }
    else {
        antd_1.message.error(res.msg || saveFailed);
    }
}
function* updateResourceFolder(action) {
    const { params, options } = action.payload;
    const { applicationId, folderId, name } = params || {};
    const res = yield (0, effects_1.call)(API.putResourcesFolders, {
        applicationId,
        id: folderId,
        name,
    });
    const { global: { updateSuccess, updateFailed }, } = (0, index_1.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(updateSuccess);
        yield (0, effects_1.put)(ACTIONS.fetchResourcesList({ applicationId }));
        const { onSuccess } = options || {};
        if (typeof onSuccess === 'function')
            onSuccess();
    }
    else {
        antd_1.message.error(res.msg || updateFailed);
    }
}
function* removeResources(action) {
    const { params } = action.payload;
    const res = yield (0, effects_1.call)(API.putResourcesStatus, Object.assign(Object.assign({}, params), { status: true }));
    const { global: { deleteSuccess, deleteFailed }, } = (0, index_1.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(deleteSuccess);
        // clear selected key
        yield (0, effects_1.put)(ACTIONS.updateSelectedRowKeys([]));
        yield (0, effects_1.put)(ACTIONS.fetchResourcesList({ applicationId: params.applicationId }));
    }
    else {
        antd_1.message.error(res.msg || deleteFailed);
    }
}
function* fetchResourceData(action) {
    yield (0, effects_1.put)(ACTIONS.updateResourcesDetailState({
        resLoading: true,
    }));
    const { params } = action.payload;
    const { applicationId, folderPath } = params;
    const { applicationId: _applicationId, folderPath: _folderPath, } = index_2.store.getState().applications.detail.resources.detail;
    const res = yield (0, effects_1.call)(API.getResourcesByPaths, {
        applicationId: applicationId || _applicationId,
        path: folderPath || _folderPath,
        depth: 1,
    });
    if (res.code === 200) {
        const resourceRootInfo = res.data || {};
        const { folders = [], files = [] } = resourceRootInfo.children || {};
        const resourceList = [
            ...folders.map((item = {}) => {
                item.key = item.key || item.id;
                item.__type = 'folder';
                delete item.children;
                return item;
            }),
            ...files.map((item = {}) => {
                item.key = item.key || item.id;
                item.__type = 'file';
                delete item.children;
                return item;
            }),
        ];
        delete resourceRootInfo.children;
        yield (0, effects_1.put)(ACTIONS.updateResourcesDetailState({
            resLoading: false,
            resourceRootInfo,
            resourceList,
        }));
    }
    else {
        const { global: { saveFailed }, } = (0, index_1.getBusinessI18n)();
        antd_1.message.error(res.msg || saveFailed);
    }
}
function* fetchGroupInfo(action) {
    const { params } = action.payload;
    const res = yield (0, effects_1.call)(API.getResourcesGroups, params);
    if (res.code === 200) {
        const groupInfo = res.data || {};
        yield (0, effects_1.put)(ACTIONS.updateResourcesDetailState({
            groupInfo,
        }));
    }
    else {
        const { global: { saveFailed }, } = (0, index_1.getBusinessI18n)();
        antd_1.message.error(res.msg || saveFailed);
    }
}
function* watch() {
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.addFile), addResourceFile);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.updateFile), updateResourceFile);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.addFolder), addResourceFolder);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.updateFolder), updateResourceFolder);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.removeResources), removeResources);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchResourcesList), fetchResourceData);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchGroupInfo), fetchGroupInfo);
}
function* rootSaga() {
    yield (0, effects_1.all)([watch()]);
}
exports.default = rootSaga;
