"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactTypeScriptFileUpdate = void 0;
const ts = require("typescript");
const cli_core_1 = require("@igniteui/cli-core");
const types_1 = require("./types");
class ReactTypeScriptFileUpdate extends cli_core_1.TypeScriptFileUpdate {
    constructor(filePath, formatSettings, compilerOptions) {
        super(filePath, formatSettings, compilerOptions);
    }
    //#region Overridden Public API
    addRoute(route, multiline = false, prepend = false, anchorElement) {
        return this.addRedirectOrSimpleRouteEntry(Object.assign(Object.assign({}, route), { identifierName: route.identifierName || route.element, name: route.name || route.text }), (0, cli_core_1.RoutesVariableAsParentCondition)(this.astTransformer), 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);
        const modulePath = `./${routeName}/${routeName}`;
        this.requestImportForRouteIdentifier(Object.assign(Object.assign({}, route), { modulePath }), true // is default - for React, we import the component as `import X from 'module'`.
        );
        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: types_1.ReactRouteTarget.Element,
                value: ts.factory.createJsxSelfClosingElement(ts.factory.createIdentifier(route.element || route.identifierName), undefined, // type arguments
                undefined // jsx attributes
                ),
            },
            {
                name: types_1.ReactRouteTarget.Text,
                value: ts.factory.createStringLiteral(route.text || 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 loaderPropertyAssignment = this.createArrowFunctionWithCallExpression(types_1.ReactRouteTarget.Loader, route.loader || cli_core_1.REACT_ROUTER_DOM_REDIRECT, route.redirectTo);
        this.requestImportForRouteLoader(route);
        const structure = [
            {
                name: types_1.ReactRouteTarget.Index,
                value: route.index ? ts.factory.createTrue() : ts.factory.createFalse(),
            },
            {
                name: types_1.ReactRouteTarget.Loader,
                value: loaderPropertyAssignment.value,
            },
        ];
        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(Object.assign(Object.assign({}, route), { identifierName: route.identifierName || route.element }), asIdentifier || true, // for React the `children` member should always be added in the form { children: identifier }
        multiline, prepend);
    }
    //#endregion
    requestImportForRouteLoader(route, isDefault) {
        return this.requestImportForRouteIdentifier(Object.assign(Object.assign({}, route), { identifierName: route.loader || cli_core_1.REACT_ROUTER_DOM_REDIRECT, modulePath: cli_core_1.REACT_ROUTER_DOM_MODULE }), isDefault);
    }
}
exports.ReactTypeScriptFileUpdate = ReactTypeScriptFileUpdate;
