"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const lib_1 = require("../../lib");
const core_1 = require("@angular-devkit/core");
const angular_utility_1 = require("../../lib/angular-utility");
function default_1(_options) {
    function createRoutes(files, routes, name, path) {
        let sourceTemplates = schematics_1.url(files);
        return angular_utility_1.applyWithOverwriteRule(sourceTemplates, [
            schematics_1.template(Object.assign({ name,
                routes }, core_1.strings)),
            schematics_1.move(path)
        ]);
    }
    return schematics_1.chain([
        (tree, _context) => {
            let project = lib_1.loadProject(tree, _context);
            const { name, path } = lib_1.getAngularProjectPath(_options.name || 'app', tree, _options);
            let routes = project.getAppRouting();
            return createRoutes('files', routes, name, path);
        },
        (tree, _context) => {
            let project = lib_1.loadProject(tree, _context);
            for (let page of project.appConfigBuilder.pageList) {
                if (page.children) {
                    const { name, path } = lib_1.getAngularProjectPath(page.projectPath, tree, _options);
                    let childrenPages = Object.values(page.children);
                    let routes = project.createRoutesForPages(page, childrenPages);
                    if (!routes.find(page => page.path === '') && childrenPages.length > 0) {
                        let defaultPagePath = childrenPages[0].path;
                        _context.logger.info(`adding default page for ${page.type}: ${defaultPagePath}`);
                        routes.push({
                            path: '',
                            redirectTo: defaultPagePath
                        });
                    }
                    return createRoutes('files-sub-router', routes, name, path);
                }
            }
        }
    ]);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map