"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const ast_utils_1 = require("./ast-utils");
function parse(content) {
    return ts.createSourceFile('test.ts', content, ts.ScriptTarget.Latest, true);
}
describe('applyChanges', () => {
    it('aplica mudanças em ordem reversa de posição', () => {
        const content = 'ABCDE';
        const result = (0, ast_utils_1.applyChanges)(content, [
            { start: 0, end: 1, replacement: 'X' },
            { start: 3, end: 4, replacement: 'Y' },
        ]);
        expect(result).toBe('XBCYE');
    });
    it('suporta inserção (start === end)', () => {
        const result = (0, ast_utils_1.applyChanges)('AB', [{ start: 1, end: 1, replacement: 'X' }]);
        expect(result).toBe('AXB');
    });
    it('suporta remoção (replacement vazio)', () => {
        const result = (0, ast_utils_1.applyChanges)('ABCD', [{ start: 1, end: 3, replacement: '' }]);
        expect(result).toBe('AD');
    });
});
describe('buildRemoveNamedImportsChanges', () => {
    it('remove import statement inteiro quando só tem o nome alvo', () => {
        const content = `import { DatePickerModule } from 'primeng/datepicker';\n@Component({}) export class A {}`;
        const { changes, removedNames } = (0, ast_utils_1.buildRemoveNamedImportsChanges)(parse(content), ['DatePickerModule'], 'primeng');
        const result = (0, ast_utils_1.applyChanges)(content, changes).replace(/\n\n+/g, '\n');
        expect(result).not.toContain('DatePickerModule');
        expect(result).not.toContain("primeng/datepicker");
        expect(removedNames).toEqual(['DatePickerModule']);
    });
    it('mantém outros imports do mesmo pacote', () => {
        const content = `import { DatePickerModule, ButtonModule } from 'primeng/components';\n@Component({}) export class A {}`;
        const { changes, removedNames } = (0, ast_utils_1.buildRemoveNamedImportsChanges)(parse(content), ['DatePickerModule'], 'primeng');
        const result = (0, ast_utils_1.applyChanges)(content, changes);
        expect(result).not.toContain('DatePickerModule');
        expect(result).toContain('ButtonModule');
        expect(removedNames).toEqual(['DatePickerModule']);
    });
    it('não toca em imports de outros pacotes', () => {
        const content = `import { Component } from '@angular/core';\n@Component({}) export class A {}`;
        const { changes } = (0, ast_utils_1.buildRemoveNamedImportsChanges)(parse(content), ['DatePickerModule'], 'primeng');
        expect(changes).toHaveSize(0);
    });
    it('remove múltiplos nomes de uma vez', () => {
        const content = `import { DatePickerModule, CalendarModule } from 'primeng/datepicker';\n@Component({}) export class A {}`;
        const { changes, removedNames } = (0, ast_utils_1.buildRemoveNamedImportsChanges)(parse(content), ['DatePickerModule', 'CalendarModule'], 'primeng');
        const result = (0, ast_utils_1.applyChanges)(content, changes).replace(/\n\n+/g, '\n');
        expect(result).not.toContain('DatePickerModule');
        expect(result).not.toContain('CalendarModule');
        expect(removedNames).toContain('DatePickerModule');
        expect(removedNames).toContain('CalendarModule');
    });
});
describe('insertImportStatement', () => {
    it('insere após o último import existente', () => {
        const content = `import { Component } from '@angular/core';\n@Component({}) export class A {}`;
        const result = (0, ast_utils_1.insertImportStatement)(content, 'MyComp', '@my/lib');
        expect(result).toContain(`import { MyComp } from '@my/lib';`);
        const importIndex = result.indexOf(`import { MyComp }`);
        const angularIndex = result.indexOf(`import { Component }`);
        expect(importIndex).toBeGreaterThan(angularIndex);
    });
    it('prepend ao arquivo quando não há imports', () => {
        const content = `@Component({}) export class A {}`;
        const result = (0, ast_utils_1.insertImportStatement)(content, 'MyComp', '@my/lib');
        expect(result.startsWith(`import { MyComp } from '@my/lib';\n`)).toBeTrue();
    });
});
describe('ngModule.replaceInImports', () => {
    it('substitui item no array imports de @NgModule', () => {
        const content = `@NgModule({ imports: [CommonModule, DatePickerModule] })\nexport class AppModule {}`;
        const changes = ast_utils_1.ngModule.replaceInImports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        const result = (0, ast_utils_1.applyChanges)(content, changes);
        expect(result).toContain('imports: [CommonModule, DatepickerComponent]');
        expect(result).not.toContain('DatePickerModule');
    });
    it('não altera decorator sem array imports', () => {
        const content = `@NgModule({ declarations: [] })\nexport class AppModule {}`;
        const changes = ast_utils_1.ngModule.replaceInImports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        expect(changes).toHaveSize(0);
    });
    it('não altera array quando o item a remover não está presente', () => {
        const content = `@NgModule({ imports: [CommonModule] })\nexport class AppModule {}`;
        const changes = ast_utils_1.ngModule.replaceInImports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        expect(changes).toHaveSize(0);
    });
    it('remove CalendarModule (alias PrimeNG legado)', () => {
        const content = `@NgModule({ imports: [CalendarModule] })\nexport class AppModule {}`;
        const changes = ast_utils_1.ngModule.replaceInImports(parse(content), ['CalendarModule'], 'DatepickerComponent');
        const result = (0, ast_utils_1.applyChanges)(content, changes);
        expect(result).toContain('DatepickerComponent');
        expect(result).not.toContain('CalendarModule');
    });
    it('não toca em @Component', () => {
        const content = `@Component({ imports: [DatePickerModule], template: '' })\nexport class AppComponent {}`;
        const changes = ast_utils_1.ngModule.replaceInImports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        expect(changes).toHaveSize(0);
    });
});
describe('ngModule.replaceInExports', () => {
    it('substitui item no array exports de @NgModule', () => {
        const content = `@NgModule({ imports: [BrowserModule], exports: [DatePickerModule, DropdownModule] })\nexport class AppModule {}`;
        const changes = ast_utils_1.ngModule.replaceInExports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        const result = (0, ast_utils_1.applyChanges)(content, changes);
        expect(result).toContain('exports: [DropdownModule, DatepickerComponent]');
        expect(result).not.toContain('DatePickerModule');
        expect(result).toContain('DropdownModule');
    });
    it('não toca em @Component', () => {
        const content = `@Component({ template: '' })\nexport class AppComponent {}`;
        const changes = ast_utils_1.ngModule.replaceInExports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        expect(changes).toHaveSize(0);
    });
});
describe('ngComponent.replaceInImports', () => {
    it('substitui item no array imports de @Component standalone', () => {
        const content = `@Component({ imports: [DatePickerModule], template: '' })\nexport class AppComponent {}`;
        const changes = ast_utils_1.ngComponent.replaceInImports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        const result = (0, ast_utils_1.applyChanges)(content, changes);
        expect(result).toContain('imports: [DatepickerComponent]');
        expect(result).not.toContain('DatePickerModule');
    });
    it('não altera decorator sem array imports', () => {
        const content = `@Component({ template: '' })\nexport class AppComponent {}`;
        const changes = ast_utils_1.ngComponent.replaceInImports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        expect(changes).toHaveSize(0);
    });
    it('não toca em @NgModule', () => {
        const content = `@NgModule({ imports: [DatePickerModule] })\nexport class AppModule {}`;
        const changes = ast_utils_1.ngComponent.replaceInImports(parse(content), ['DatePickerModule'], 'DatepickerComponent');
        expect(changes).toHaveSize(0);
    });
});
//# sourceMappingURL=ast-utils.test.js.map