"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
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/workspace/projects/personal/content"));
const APPLICATION_API = __importStar(require("@/apis/application"));
const AUTH_API = __importStar(require("@/apis/authorize"));
const BUILDER_API = __importStar(require("@/apis/builder"));
const API = __importStar(require("@/apis/project"));
const index_1 = require("@/constants/index");
const index_2 = require("@/foxI18n/index");
const index_3 = require("@/store/index");
const short_id_1 = __importDefault(require("@/utils/short-id"));
function* handleFetchList(action) {
    yield (0, effects_1.put)(ACTIONS.updateLoading(true));
    const { applicationId, fileId, fileType } = action.payload;
    const res = yield (0, effects_1.call)(fileType === 'page' ? API.fetchPageContents : API.fetchTemplateContents, {
        applicationId,
        fileId,
    });
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushContentList(res.data));
    }
    else {
        const { content: { fetchFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateLoading(false));
}
function* handleSave(action) {
    yield (0, effects_1.put)(ACTIONS.updateSaveLoading(true));
    const { applicationId, fileId, fileType } = action.payload;
    const { editContent: content } = index_3.store.getState().workspace.projects.personal.content;
    const saveApi = content.id
        ? fileType === index_1.FileType.page
            ? API.updatePageContent
            : API.updateTemplateContent
        : fileType === index_1.FileType.page
            ? API.addPageContent
            : API.addTemplateContent;
    const res = yield (0, effects_1.call)(saveApi, Object.assign(Object.assign({}, content), { extendId: !content.extendId ? undefined : content.extendId, fileId,
        applicationId }));
    const { global: { saveSuccess, saveFailed, addFailed }, } = (0, index_2.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(saveSuccess);
        yield (0, effects_1.put)(ACTIONS.openEditDrawer(false));
        yield (0, effects_1.put)(ACTIONS.fetchContentList({ applicationId, fileId, fileType }));
        // add root as default for content new base page/new locale page without extend
        if (fileType === index_1.FileType.page &&
            !content.id &&
            (content.isBase || (!content.isBase && !content.extendId))) {
            // TODO: need to clean
            const addRootRes = yield (0, effects_1.call)(BUILDER_API.updatePageDsl, {
                applicationId,
                content: {
                    id: res.data.id,
                    relation: {},
                    schemas: [
                        {
                            children: [],
                            id: `stru_${(0, short_id_1.default)(15)}`,
                            name: 'page',
                            props: { width: '100%', height: '100%' },
                            type: 'page',
                        },
                    ],
                },
                id: res.data.id,
            });
            if (addRootRes.code !== 200) {
                antd_1.message.error(addRootRes.msg || addFailed);
            }
        }
    }
    else {
        antd_1.message.error(res.msg || saveFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateSaveLoading(false));
}
function* handleDelete(action) {
    const { applicationId, id, status, fileId, fileType } = action.payload;
    const res = yield (0, effects_1.call)(fileType === 'page' ? API.deletePageContent : API.deleteTemplateContent, {
        applicationId,
        id,
        status,
    });
    const { global: { deleteSuccess, deleteFailed }, } = (0, index_2.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(deleteSuccess);
        yield (0, effects_1.put)(ACTIONS.fetchContentList({ applicationId: applicationId || '', fileId: fileId || '', fileType }));
    }
    else {
        antd_1.message.error(res.msg || deleteFailed);
    }
}
function* handleFetchLocales(action) {
    const { applicationId } = action.payload;
    const res = yield (0, effects_1.call)(APPLICATION_API.fetchAppDetail, {
        applicationId,
    });
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushLocales(res.data.locales));
    }
    else {
        const { application: { fetchLocalesFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchLocalesFailed);
    }
}
function* handleCommitToStore(action) {
    const { params, cb } = action.payload;
    const res = yield (0, effects_1.call)(API.commitToStore, params);
    const { global: { commitSuccess, commitFailed }, } = (0, index_2.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(commitSuccess);
        if (typeof cb === 'function')
            cb();
    }
    else {
        antd_1.message.error(res.msg || commitFailed);
    }
}
function* handleOfflineFromStore(action) {
    const { params, cb } = action.payload;
    const res = yield (0, effects_1.call)(API.offlineFromStore, params);
    const { global: { revokeSuccess, revokeFailed }, } = (0, index_2.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(revokeSuccess);
        if (typeof cb === 'function')
            cb();
    }
    else {
        antd_1.message.error(res.msg || revokeFailed);
    }
}
function* handleFetchFileDetail(action) {
    var _a;
    yield (0, effects_1.put)(ACTIONS.updateLoading(true));
    const { params } = action.payload;
    const res = yield (0, effects_1.call)(API.fetchFileDetail, params);
    if (res.code === 200 && ((_a = res.data) === null || _a === void 0 ? void 0 : _a.length) > 0) {
        yield (0, effects_1.put)(ACTIONS.pushFileDetail(res.data[0]));
    }
    else {
        const { file: { fetchDetailFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchDetailFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateLoading(false));
}
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 res = yield (0, effects_1.call)(AUTH_API.authorizeFetch, params);
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushAuthList(res.data || []));
    }
    else {
        const { global: { fetchListFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchListFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateAuthListLoading(false));
}
function* handleSaveAuth(action) {
    const { params, cb } = action.payload;
    const res = yield (0, effects_1.call)(AUTH_API.authorizeAdd, params);
    if (res.code === 200) {
        if (typeof cb === 'function')
            cb();
    }
    else {
        const { global: { addFailed }, } = (0, index_2.getBusinessI18n)();
        antd_1.message.error(res.msg || addFailed);
    }
}
function* handleDeleteAuth(action) {
    const { params, cb } = action.payload;
    const res = yield (0, effects_1.call)(AUTH_API.authorizeDelete, params);
    const { global: { deleteSuccess, deleteFailed }, } = (0, index_2.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(deleteSuccess);
        if (typeof cb === 'function')
            cb();
    }
    else {
        antd_1.message.error(res.msg || deleteFailed);
    }
}
function* handleFetchAuthUserList(action) {
    const { params } = action.payload;
    const res = yield (0, effects_1.call)(AUTH_API.authorizeUserFetch, params);
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.pushUserList(res.data || []));
    }
    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.fetchContentList), handleFetchList);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.saveContent), handleSave);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.deleteContent), handleDelete);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchLocales), handleFetchLocales);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.commitFileToStore), handleCommitToStore);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.offlineFileFromStore), handleOfflineFromStore);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchFileDetail), handleFetchFileDetail);
    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;
