"use strict";
// © 2022 SolarWinds Worldwide, LLC. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to
//  deal in the Software without restriction, including without limitation the
//  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
//  sell copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const find_module_1 = require("@schematics/angular/utility/find-module");
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
const parse_name_1 = require("@schematics/angular/utility/parse-name");
const validation_1 = require("@schematics/angular/utility/validation");
const project_1 = require("../utility/project");
const schematics_helper_1 = require("../utility/schematics-helper");
function addModule(host, options) {
    const projectTargets = (0, schematics_helper_1.getBrowserProjectTargets)(host, options);
    const mainPath = projectTargets.options.main;
    const appModulePath = (0, ng_ast_utils_1.getAppModulePath)(host, mainPath);
    const moduleSource = (0, schematics_helper_1.readIntoSourceFile)(host, appModulePath);
    const modulePath = options.module;
    const importedModulePath = `/${options.path}/` +
        (options.flat ? "" : core_1.strings.dasherize(options.name) + "/") +
        core_1.strings.dasherize(options.name) +
        ".module";
    const relativePath = (0, find_module_1.buildRelativePath)(modulePath, importedModulePath);
    (0, schematics_helper_1.updateModuleChanges)(host, options, moduleSource, [
        {
            item: `${core_1.strings.classify(options.name)}Module`,
            path: relativePath,
        },
        {
            item: "BrowserAnimationsModule",
            path: "@angular/platform-browser/animations",
        },
    ], [{ item: "DatePipe", path: "@angular/common" }]);
}
function default_1(options) {
    return (host) => {
        if (!options.project) {
            throw new schematics_1.SchematicsException("Option (project) is required.");
        }
        const project = (0, project_1.getProject)(host, options.project);
        if (options.path === undefined) {
            options.path = (0, project_1.buildDefaultPath)(project);
        }
        options.module = (0, find_module_1.findModuleFromOptions)(host, options);
        const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
        options.name = parsedPath.name;
        options.path = parsedPath.path;
        options.selector =
            options.selector || (0, schematics_helper_1.buildSelector)(options, project.prefix);
        options.prefix = options.prefix ?? project.prefix;
        if (!options.skipImport) {
            addModule(host, options);
        }
        (0, validation_1.validateHtmlSelector)(options.selector);
        const templateSource = (0, schematics_1.apply)((0, schematics_1.url)("./files"), [
            (0, schematics_1.template)({
                ...core_1.strings,
                "if-flat": (s) => (options.flat ? "" : s),
                ...options,
            }),
            (0, schematics_1.move)(parsedPath.path),
        ]);
        return (0, schematics_1.chain)([(0, schematics_1.mergeWith)(templateSource)]);
    };
}
//# sourceMappingURL=index.js.map