"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 effects_1 = require("redux-saga/effects");
const typesafe_actions_1 = require("typesafe-actions");
const ACTIONS = __importStar(require("@/actions/builder/main"));
const index_1 = require("@/store/index");
const services_1 = require("./services");
function* handlePreStep() {
    const { curStep, content } = index_1.store.getState().builder.main;
    const steps = (0, services_1.getCached)(content.id);
    if (curStep > 0) {
        const preStep = curStep - 1;
        const step = steps[preStep];
        if (step) {
            yield (0, effects_1.put)(ACTIONS.goStep(step));
        }
        yield (0, effects_1.put)(ACTIONS.setCurStep(preStep));
    }
}
function* handleNextStep() {
    const { curStep = 0, content } = index_1.store.getState().builder.main;
    const steps = (0, services_1.getCached)(content.id);
    if (curStep < steps.length) {
        const nextStep = curStep + 1;
        const step = steps[nextStep];
        if (step) {
            yield (0, effects_1.put)(ACTIONS.goStep(step));
        }
        yield (0, effects_1.put)(ACTIONS.setCurStep(nextStep));
    }
}
function* handleDoStep(actions) {
    const { params } = actions.payload;
    // @ts-ignore
    const state = yield (0, effects_1.call)(services_1.initState, params, {
        application: index_1.store.getState().builder.main.application,
        locale: index_1.store.getState().builder.header.locale,
        components: index_1.store.getState().builder.component.components,
        extendPage: index_1.store.getState().builder.main.extend,
        file: index_1.store.getState().builder.main.file,
    });
    yield (0, effects_1.put)(ACTIONS.pushContent(params));
    yield (0, effects_1.put)(ACTIONS.pushFormatData(state));
    yield (0, effects_1.put)(ACTIONS.updateEditState(true));
}
function* handleClearStep() {
    (0, services_1.clearCache)();
}
function* handleGetSteps() {
    const { content } = index_1.store.getState().builder.main;
    const steps = (0, services_1.getCached)(content.id);
    yield (0, effects_1.put)(ACTIONS.setSteps(steps.length));
    yield (0, effects_1.put)(ACTIONS.setCurStep(steps.length));
}
function* handlePushStep(actions) {
    const { params } = actions.payload;
    const cached = (0, services_1.getCached)(params.contentId);
    if (cached.length === 0) {
        const { pageContent } = index_1.store.getState().builder.main;
        if (pageContent.contentId) {
            // cache origin first
            (0, services_1.cache)(pageContent);
        }
    }
    // cache
    const count = (0, services_1.cache)(params);
    yield (0, effects_1.put)(ACTIONS.setCurStep(count > 0 ? count - 1 : 0));
    yield (0, effects_1.put)(ACTIONS.setSteps(count));
}
function* watch() {
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.preStep), handlePreStep);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.nextStep), handleNextStep);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.goStep), handleDoStep);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.getSteps), handleGetSteps);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.pushStep), handlePushStep);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.clearAll), handleClearStep);
    yield (0, effects_1.takeLatest)((0, typesafe_actions_1.getType)(ACTIONS.clearByContentChange), handleClearStep);
}
function* rootSaga() {
    yield (0, effects_1.all)([watch()]);
}
exports.default = rootSaga;
