"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 });
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const change_1 = require("@schematics/angular/utility/change");
const find_module_1 = require("@schematics/angular/utility/find-module");
const lint_fix_1 = require("@schematics/angular/utility/lint-fix");
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 addDeclarationToNgModule(options) {
    return (host) => {
        if (options.skipImport || !options.module) {
            return host;
        }
        const modulePath = options.module;
        let moduleSource = schematics_helper_1.readIntoSourceFile(host, modulePath);
        const componentPath = `/${options.path}/`
            + (options.flat ? "" : core_1.strings.dasherize(options.name) + "/")
            + core_1.strings.dasherize(options.name)
            + ".component";
        const relativePath = find_module_1.buildRelativePath(modulePath, componentPath);
        const componentName = core_1.strings.classify(`${options.name}Component`);
        // default modules to be imported
        const modules = [
            { item: "NuiSorterModule", path: "@nova-ui/bits" },
            { item: "NuiRepeatModule", path: "@nova-ui/bits" },
        ];
        // additional module(s) used only for search functionality
        if (options.enableSearch) {
            modules.push({ item: "NuiSearchModule", path: "@nova-ui/bits" });
        }
        // additional module(s) used only for pagination
        if (options.pagingMode === "pagination") {
            modules.push({ item: "NuiPaginatorModule", path: "@nova-ui/bits" });
        }
        // additional module(s) used only for virtual scroll
        if (options.dataSource === "serverSide") {
            if (options.dataSource === "serverSide") {
                modules.push({ item: "HttpClientModule", path: "@angular/common/http" });
            }
            modules.push({ item: "NuiBusyModule", path: "@nova-ui/bits" }, { item: "NuiSpinnerModule", path: "@nova-ui/bits" });
        }
        schematics_helper_1.updateModuleChanges(host, options, moduleSource, modules, [], [{ item: componentName, path: relativePath }]);
        if (options.export) {
            // Need to refresh the AST because we overwrote the file in the host.
            moduleSource = schematics_helper_1.readIntoSourceFile(host, modulePath);
            const exportRecorder = host.beginUpdate(modulePath);
            const exportChanges = ast_utils_1.addExportToModule(moduleSource, modulePath, core_1.strings.classify(`${options.name}Component`), relativePath);
            for (const change of exportChanges) {
                if (change instanceof change_1.InsertChange) {
                    exportRecorder.insertLeft(change.pos, change.toAdd);
                }
            }
            host.commitUpdate(exportRecorder);
        }
        return host;
    };
}
function default_1(options) {
    return (host) => {
        if (!options.project) {
            throw new schematics_1.SchematicsException("Option (project) is required.");
        }
        const project = project_1.getProject(host, options.project);
        if (options.path === undefined) {
            options.path = project_1.buildDefaultPath(project);
        }
        options.module = find_module_1.findModuleFromOptions(host, options);
        const parsedPath = parse_name_1.parseName(options.path, options.name);
        options.name = parsedPath.name;
        options.path = parsedPath.path;
        options.selector = options.selector || schematics_helper_1.buildSelector(options, project.prefix);
        options.dataSourceName = options.dataSourceName || options.name;
        validation_1.validateName(options.name);
        validation_1.validateHtmlSelector(options.selector);
        const templateSource = schematics_1.apply(schematics_1.url("./files"), [
            schematics_1.template(Object.assign(Object.assign(Object.assign({}, core_1.strings), { "if-flat": (s) => options.flat ? "" : s }), options)),
            schematics_1.filter((path) => {
                if (options.dataSource === "custom" && (path.endsWith("data-source.service.ts") || path.endsWith("types.ts"))) {
                    return false;
                }
                if (options.dataSource === "none" && (path.endsWith("data-source.service.ts"))) {
                    return false;
                }
                return (options.dataSourceName === options.name)
                    ||
                        (options.dataSourceName !== options.name
                            && !path.endsWith("data-source.service.ts")
                            && !path.endsWith("types.ts")
                            && !path.endsWith("data.ts"));
            }),
            schematics_1.move(`${parsedPath.path}/${parsedPath.name}`),
        ]);
        return schematics_1.chain([
            schematics_1.branchAndMerge(schematics_1.chain([
                addDeclarationToNgModule(options),
                schematics_1.mergeWith(templateSource),
            ])),
            options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
        ]);
    };
}
exports.default = default_1;
//# sourceMappingURL=index.js.map