"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyDefaultSlide = exports.slideExists = exports.slideExistsAssert = exports.createSlide = exports.getSuccesSlides$ = exports.createSlide$ = exports.createSlides$ = void 0;
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const validateId_1 = require("../utils/validateId");
const WizError_1 = require("../modules/WizError");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const cloneTemplate_1 = require("./cloneTemplate");
const TemplateTypesList_1 = require("../modules/TemplateTypesList");
const system_settings_1 = require("@ewizardjs/system-settings");
function createSlides$(type, dir, slides) {
    return (0, rxjs_1.of)(type)
        .pipe((0, operators_1.map)((type) => (0, TemplateTypesList_1.getTemplateData)({ type })), (0, operators_1.flatMap)(getCloneArguments(dir)), (0, operators_1.flatMap)(cloneTemplate_1.default), (0, operators_1.mergeMap)((path) => (0, rxjs_1.from)(slides)
        .pipe((0, operators_1.concatMap)(createSlide$(path, dir)))));
}
exports.createSlides$ = createSlides$;
function getCloneArguments(presentationDir) {
    return (template) => __awaiter(this, void 0, void 0, function* () {
        const templatePath = (0, path_1.join)(presentationDir, template.root);
        const versionHash = yield (0, system_settings_1.getByPath)(templatePath, 'template.version');
        return {
            versionHash,
            template,
        };
    });
}
function createSlide$(templatePath, dir) {
    return slideId => (0, rxjs_1.defer)(() => createSlide(templatePath, dir, slideId))
        .pipe((0, operators_1.catchError)((err) => (0, rxjs_1.of)({
        slide: slideId,
        status: 'error',
        message: err.message,
    })));
}
exports.createSlide$ = createSlide$;
function getSuccesSlides$(generation$) {
    return generation$.pipe((0, operators_1.filter)(isSucces), (0, operators_1.map)(info => info.slide), (0, operators_1.toArray)(), (0, operators_1.defaultIfEmpty)([]));
}
exports.getSuccesSlides$ = getSuccesSlides$;
function isSucces(info) {
    return info.status === 'success';
}
function createSlide(templatePath, dir, slideId) {
    return __awaiter(this, void 0, void 0, function* () {
        const pathToTemplateSlide = (0, path_1.resolve)(templatePath, 'template/slides/default');
        const destinationPath = (0, path_1.resolve)(dir, `slides/${slideId}`);
        yield (0, validateId_1.validateId)(slideId);
        const isSlideExists = yield slideExists(destinationPath);
        slideExistsAssert(isSlideExists, slideId);
        yield copyDefaultSlide(pathToTemplateSlide, destinationPath);
        return {
            slide: slideId,
            status: 'success',
        };
    });
}
exports.createSlide = createSlide;
function slideExistsAssert(exists, slideId) {
    if (exists) {
        throw new WizError_1.WizError('SLIDE_EXISTS', slideId);
    }
}
exports.slideExistsAssert = slideExistsAssert;
function slideExists(path) {
    return (0, fs_extra_1.pathExists)(path);
}
exports.slideExists = slideExists;
function copyDefaultSlide(pathToTemplateSlide, destinationPath) {
    return __awaiter(this, void 0, void 0, function* () {
        const isExist = yield (0, fs_extra_1.pathExists)(destinationPath);
        if (!isExist) {
            return (0, fs_extra_1.copy)(pathToTemplateSlide, destinationPath);
        }
    });
}
exports.copyDefaultSlide = copyDefaultSlide;
//# sourceMappingURL=slideGenerator.js.map