"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/packages/detail"));
const API = __importStar(require("@/apis/application"));
const PROJECT_API = __importStar(require("@/apis/project"));
const index_1 = require("@/foxI18n/index");
const index_2 = require("@/store/index");
function* fetchComponentInfo(action) {
    yield (0, effects_1.put)(ACTIONS.updateLoading(true));
    const { params, options = {} } = action.payload;
    const { applicationId, id } = params || {};
    const res = yield (0, effects_1.call)(API.getComponentsContents, {
        applicationId,
        id,
    });
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.updateComponentInfoState(res.data || {}));
        const { onSuccess } = options;
        if (typeof onSuccess === 'function')
            onSuccess();
    }
    else {
        const { component: { fetchDetailFailed }, } = (0, index_1.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchDetailFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateLoading(false));
}
function* addVersion(action) {
    const { params, options = {} } = action.payload;
    const res = yield (0, effects_1.call)(API.postComponentsVersions, params);
    if (res.code === 200) {
        const { onSuccess } = options;
        if (typeof onSuccess === 'function')
            onSuccess();
        yield (0, effects_1.put)(ACTIONS.fetchComponentVersionsAction({}));
    }
    else {
        antd_1.message.error(res.msg);
    }
}
function* editVersion(action) {
    const { params, options = {} } = action.payload;
    const res = yield (0, effects_1.call)(API.putComponentsVersions, params);
    if (res.code === 200) {
        const { onSuccess } = options;
        if (typeof onSuccess === 'function')
            onSuccess();
        yield (0, effects_1.put)(ACTIONS.fetchComponentVersionsAction({}));
    }
    else {
        antd_1.message.error(res.msg);
    }
}
function* fetchVersionList(action) {
    yield (0, effects_1.put)(ACTIONS.updateVersionListState({
        loading: true,
    }));
    const { options = {} } = action.payload;
    const { applicationId, fileId, pageInfo, } = index_2.store.getState().applications.detail.packages.detail.versionList;
    const { size = 10, page = 1 } = pageInfo;
    const res = yield (0, effects_1.call)(API.getComponentsVersionSearchs, {
        applicationId,
        id: fileId,
        size,
        page,
    });
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.updateVersionListState({
            loading: false,
            versions: res.data || [],
            pageInfo: res.pageInfo || {
                page: 1,
                total: 0,
                size: 10,
            },
        }));
        const { onSuccess } = options;
        if (typeof onSuccess === 'function')
            onSuccess();
    }
    else {
        antd_1.message.error(res.msg);
    }
}
function* updateVersionStatus(action) {
    const { params, options = {} } = action.payload;
    const res = yield (0, effects_1.call)(API.putComponentsVersionPublish, params);
    const { global: { updateFailed, updateSuccess }, } = (0, index_1.getBusinessI18n)();
    if (res.code === 200) {
        antd_1.message.success(updateSuccess);
        yield (0, effects_1.put)(ACTIONS.fetchComponentVersionsAction({}));
        const { onSuccess } = options;
        if (typeof onSuccess === 'function')
            onSuccess();
    }
    else {
        antd_1.message.error(res.msg || updateFailed);
    }
}
function* liveVersion(action) {
    const { params, options = {} } = action.payload;
    const res = yield (0, effects_1.call)(API.putComponentsLiveVersion, params);
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.fetchComponentVersionsAction({}));
        const { onSuccess } = options;
        if (typeof onSuccess === 'function')
            onSuccess();
    }
    else {
        antd_1.message.error(res.msg);
    }
}
function* handleFetchFileDetail(action) {
    var _a;
    const { applicationId, ids } = action.payload;
    const res = yield (0, effects_1.call)(PROJECT_API.fetchFileDetail, {
        applicationId,
        ids,
    });
    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_1.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchDetailFailed);
    }
}
function* handleFetchComponentRemotes(action) {
    const params = action.payload;
    const res = yield (0, effects_1.call)(API.searchComponentRemotes, params);
    if (res.code === 200 && res.data) {
        const { components, lastVersion } = res.data;
        yield (0, effects_1.put)(ACTIONS.pushComponentRemotes((components === null || components === void 0 ? void 0 : components.length) > 0 ? components[0] : undefined, lastVersion));
    }
    else {
        const { component: { fetchUpdateInfoFailed }, } = (0, index_1.getBusinessI18n)();
        antd_1.message.error(res.msg || fetchUpdateInfoFailed);
    }
}
function* handleSaveComponentRemote(action) {
    yield (0, effects_1.put)(ACTIONS.updateCloudSyncDrawerLoadingStatus(true));
    const params = action.payload;
    const res = yield (0, effects_1.call)(API.saveComponentRemote, params);
    if (res.code === 200) {
        yield (0, effects_1.put)(ACTIONS.updateCloudSyncDrawerOpenStatus(false));
        yield (0, effects_1.put)(ACTIONS.fetchComponentVersionsAction({}));
    }
    else {
        const { global: { saveFailed }, } = (0, index_1.getBusinessI18n)();
        antd_1.message.error(res.msg || saveFailed);
    }
    yield (0, effects_1.put)(ACTIONS.updateCloudSyncDrawerLoadingStatus(false));
}
function* watch() {
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchComponentInfoAction), fetchComponentInfo);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.addComponentVersionAction), addVersion);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.editComponentVersionAction), editVersion);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchComponentVersionsAction), fetchVersionList);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.updateComponentVersionStatusAction), updateVersionStatus);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.liveComponentVersionAction), liveVersion);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchFileDetail), handleFetchFileDetail);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.fetchComponentRemotes), handleFetchComponentRemotes);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.saveComponentRemote), handleSaveComponentRemote);
}
function* rootSaga() {
    yield (0, effects_1.all)([watch()]);
}
exports.default = rootSaga;
