"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebComponentsTypeScriptFileUpdate = void 0;
const ts = require("typescript");
const cli_core_1 = require("@igniteui/cli-core");
class WebComponentsTypeScriptFileUpdate extends cli_core_1.TypeScriptFileUpdate {
    constructor(filePath, formatSettings) {
        super(filePath, formatSettings);
        this.filePath = filePath;
    }
    //#region Overridden Public API
    addRoute(route, multiline = false, prepend = true, anchorElement = cli_core_1.WC_ANCHOR_ELEMENT) {
        return this.addRedirectOrSimpleRouteEntry(route, (0, cli_core_1.RoutesVariableAsParentCondition)(this.astTransformer), multiline, prepend, anchorElement);
    }
    addChildRoute(parentPath, route, asIdentifier = true, multiline, prepend, anchorElement) {
        return super.addChildRoute(parentPath, route, asIdentifier, multiline, prepend, anchorElement);
    }
    //#endregion
    //#region Overrides
    addRouteEntry(route, visitCondition, multiline = false, prepend = false, anchorElement) {
        const routeName = cli_core_1.Util.lowerDashed(route.name || route.path);
        this.requestSideEffectsImportForModule(`./${routeName}/${routeName}`);
        if (route.children &&
            !Array.isArray(route.children) &&
            (route.children.identifierName || route.children.aliasName)) {
            this.requestImportForRouteIdentifier(route.children);
        }
        const structure = [
            {
                name: cli_core_1.RouteTarget.Path,
                value: ts.factory.createStringLiteral(route.path),
            },
            {
                name: cli_core_1.RouteTarget.Component,
                value: ts.factory.createStringLiteral(route.aliasName || route.identifierName),
            },
            {
                name: cli_core_1.RouteTarget.Name,
                value: ts.factory.createStringLiteral(route.name),
            },
        ];
        const newRoute = this.astTransformer.createObjectLiteralExpression(structure, multiline);
        return this.astTransformer.addMembersToArrayLiteral(visitCondition, [newRoute], prepend, anchorElement);
    }
    addRedirectRouteEntry(route, visitCondition, multiline = false, prepend = false, anchorElement) {
        const routeName = cli_core_1.Util.lowerDashed(route.name || route.path);
        this.requestSideEffectsImportForModule(`./${routeName}/${routeName}`);
        const childRoute = route.children;
        if (childRoute) {
            this.requestImportForRouteIdentifier(childRoute);
        }
        const structure = [
            {
                name: cli_core_1.RouteTarget.Path,
                value: ts.factory.createStringLiteral(route.path || ''),
            },
            {
                // vaadin/router uses the component's tag name when navigating
                name: cli_core_1.RouteTarget.Component,
                value: ts.factory.createStringLiteral(route.redirectTo),
            },
            {
                name: cli_core_1.RouteTarget.Name,
                value: ts.factory.createStringLiteral(route.name),
            },
        ];
        if (childRoute) {
            structure.push({
                // the identifier name in this case will be the alias of the routes variable
                name: cli_core_1.RouteTarget.Children,
                value: ts.factory.createIdentifier(childRoute.aliasName || childRoute.identifierName),
            });
        }
        const newRoute = this.astTransformer.createObjectLiteralExpression(structure, multiline);
        return this.astTransformer.addMembersToArrayLiteral(visitCondition, [newRoute], prepend, anchorElement);
    }
    addChildRouteEntry(route, asIdentifier = true, multiline = false, prepend = false) {
        return super.addChildRouteEntry(route, asIdentifier || true, // for WC the `children` member should always be added in the form { children: identifier }
        multiline, prepend);
    }
}
exports.WebComponentsTypeScriptFileUpdate = WebComponentsTypeScriptFileUpdate;
