"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testing_1 = require("@angular-devkit/schematics/testing");
const schematics_1 = require("@angular-devkit/schematics");
const path = require("path");
const migrationsPath = path.join(__dirname, '../../migrations.json');
const runner = new testing_1.SchematicTestRunner('migrations', migrationsPath);
const MIGRATION = 'migration-v19-6-0-p-editor';
async function runMigration(files) {
    const tree = new testing_1.UnitTestTree(schematics_1.Tree.empty());
    for (const [filePath, content] of Object.entries(files)) {
        tree.create(filePath, content);
    }
    return runner.runSchematic(MIGRATION, {}, tree);
}
describe('migrate-p-editor', () => {
    // ─── Detecção condicional ─────────────────────────────────────────────────
    it('não deve modificar arquivos quando não há p-editor no projeto', async () => {
        const content = `<div><p>Olá mundo</p></div>`;
        const result = await runMigration({ 'app.component.html': content });
        expect(result.readText('app.component.html')).toBe(content);
    });
    it('não deve modificar .ts sem EditorModule nem type editor', async () => {
        const content = `import { Component } from '@angular/core';\n@Component({}) export class AppComponent {}`;
        const result = await runMigration({ 'app.component.ts': content });
        expect(result.readText('app.component.ts')).toBe(content);
    });
    // ─── Tag rename ───────────────────────────────────────────────────────────
    it('deve renomear a tag de abertura <p-editor> para <s-editor>', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [(ngModel)]="text"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain('<s-editor');
        expect(result.readText('app.component.html')).not.toContain('<p-editor');
    });
    it('deve renomear a tag de fechamento </p-editor> para </s-editor>', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [(ngModel)]="text"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain('</s-editor>');
        expect(result.readText('app.component.html')).not.toContain('</p-editor>');
    });
    // ─── Atributos mantidos ───────────────────────────────────────────────────
    it('deve manter [style]', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [style]="{'height':'320px'}"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain(`[style]="{'height':'320px'}"`);
    });
    it('deve manter [readonly]', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [readonly]="true"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain('[readonly]="true"');
    });
    it('deve manter placeholder', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor placeholder="Digite aqui"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain('placeholder="Digite aqui"');
    });
    it('deve manter [formats]', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [formats]="formats"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain('[formats]="formats"');
    });
    // ─── Atributos removidos ──────────────────────────────────────────────────
    it('deve remover [modules]', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [modules]="quillModules"></p-editor>`,
        });
        expect(result.readText('app.component.html')).not.toContain('modules');
    });
    it('deve remover [bounds]', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [bounds]="el"></p-editor>`,
        });
        expect(result.readText('app.component.html')).not.toContain('bounds');
    });
    it('deve remover [scrollingContainer]', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [scrollingContainer]="container"></p-editor>`,
        });
        expect(result.readText('app.component.html')).not.toContain('scrollingContainer');
    });
    it('deve remover [debug]', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor [debug]="'warn'"></p-editor>`,
        });
        expect(result.readText('app.component.html')).not.toContain('debug');
    });
    // ─── Eventos renomeados ───────────────────────────────────────────────────
    it('deve renomear (onTextChange) para (textChange)', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor (onTextChange)="onChange($event)"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain('(textChange)="onChange($event)"');
        expect(result.readText('app.component.html')).not.toContain('onTextChange');
    });
    it('deve renomear (onSelectionChange) para (selectionChange)', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor (onSelectionChange)="onSel($event)"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain('(selectionChange)="onSel($event)"');
        expect(result.readText('app.component.html')).not.toContain('onSelectionChange');
    });
    it('deve renomear (onInit) para (editorInit)', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor (onInit)="onInit($event)"></p-editor>`,
        });
        expect(result.readText('app.component.html')).toContain('(editorInit)="onInit($event)"');
        expect(result.readText('app.component.html')).not.toContain('(onInit)');
    });
    // ─── Remoção de <p-header> ────────────────────────────────────────────────
    it('deve remover bloco <p-header>...</p-header>', async () => {
        const result = await runMigration({
            'app.component.html': `
<p-editor [(ngModel)]="text" [style]="{'height':'320px'}">
    <p-header>
        <span class="ql-formats">
            <button class="ql-bold"></button>
        </span>
    </p-header>
</p-editor>`,
        });
        const html = result.readText('app.component.html');
        expect(html).not.toContain('<p-header>');
        expect(html).not.toContain('</p-header>');
        expect(html).not.toContain('ql-formats');
        expect(html).toContain('<s-editor');
    });
    it('deve remover bloco ng-template com pTemplate="header"', async () => {
        const result = await runMigration({
            'app.component.html': `
<p-editor [(ngModel)]="text">
    <ng-template pTemplate="header">
        <button class="ql-bold"></button>
    </ng-template>
</p-editor>`,
        });
        const html = result.readText('app.component.html');
        expect(html).not.toContain('pTemplate="header"');
        expect(html).not.toContain('ql-bold');
        expect(html).toContain('<s-editor');
    });
    // ─── Template completo ────────────────────────────────────────────────────
    it('deve migrar template completo com múltiplos atributos e eventos', async () => {
        const result = await runMigration({
            'app.component.html': `
<p-editor
    [(ngModel)]="text"
    [style]="{'height':'320px'}"
    [readonly]="isReadonly"
    [formats]="editorFormats"
    [modules]="quillConfig"
    (onTextChange)="onChange($event)"
    (onSelectionChange)="onSel($event)"
    (onInit)="onInit($event)"
></p-editor>`,
        });
        const html = result.readText('app.component.html');
        expect(html).toContain('<s-editor');
        expect(html).toContain('[style]="');
        expect(html).toContain('[readonly]="isReadonly"');
        expect(html).toContain('[formats]="editorFormats"');
        expect(html).toContain('(textChange)="onChange($event)"');
        expect(html).toContain('(selectionChange)="onSel($event)"');
        expect(html).toContain('(editorInit)="onInit($event)"');
        expect(html).not.toContain('p-editor');
        expect(html).not.toContain('modules');
    });
    // ─── TypeScript: EditorModule → EditorComponent ───────────────────────────
    it('deve remover import EditorModule do PrimeNG', async () => {
        const result = await runMigration({
            'app.component.ts': `import { EditorModule } from 'primeng/editor';\n@Component({}) export class AppComponent {}`,
        });
        expect(result.readText('app.component.ts')).not.toContain('EditorModule');
        expect(result.readText('app.component.ts')).not.toContain("from 'primeng/editor'");
    });
    it('deve adicionar import EditorComponent do s-editor', async () => {
        const result = await runMigration({
            'app.component.ts': `import { EditorModule } from 'primeng/editor';\n@Component({}) export class AppComponent {}`,
        });
        const ts = result.readText('app.component.ts');
        expect(ts).toContain('EditorComponent');
        expect(ts).toContain('@seniorsistemas/angular-components/editor');
    });
    it('deve preservar outros imports do mesmo arquivo ao remover EditorModule', async () => {
        const result = await runMigration({
            'app.component.ts': `import { EditorModule } from 'primeng/editor';\nimport { CommonModule } from '@angular/common';\n@Component({}) export class AppComponent {}`,
        });
        const ts = result.readText('app.component.ts');
        expect(ts).toContain('CommonModule');
        expect(ts).not.toContain('EditorModule');
    });
    it('deve substituir EditorModule no array imports de @NgModule', async () => {
        const result = await runMigration({
            'app.module.ts': `import { EditorModule } from 'primeng/editor';\n@NgModule({ imports: [CommonModule, EditorModule] })\nexport class AppModule {}`,
        });
        const ts = result.readText('app.module.ts');
        expect(ts).toContain('EditorComponent');
        expect(ts).toContain('CommonModule');
        expect(ts).not.toContain('EditorModule');
    });
    it('deve substituir EditorModule no array exports de @NgModule', async () => {
        const result = await runMigration({
            'app.module.ts': `import { EditorModule } from 'primeng/editor';\n@NgModule({ imports: [BrowserModule], exports: [EditorModule, DropdownModule] })\nexport class AppModule {}`,
        });
        const ts = result.readText('app.module.ts');
        expect(ts).toContain('EditorComponent');
        expect(ts).toContain('DropdownModule');
        expect(ts).not.toContain('EditorModule');
    });
    it('deve substituir EditorModule no array imports de @Component standalone', async () => {
        const result = await runMigration({
            'app.component.ts': `import { EditorModule } from 'primeng/editor';\n@Component({ imports: [EditorModule], template: '' })\nexport class AppComponent {}`,
        });
        const ts = result.readText('app.component.ts');
        expect(ts).toContain('EditorComponent');
        expect(ts).not.toContain('EditorModule');
    });
    it('deve substituir EditorModule importado via re-export (sem import direto do primeng)', async () => {
        const result = await runMigration({
            'feature.module.ts': `import { NgModule } from '@angular/core';\nimport { EditorModule } from '../shared/primeng';\n\n@NgModule({ imports: [EditorModule] })\nexport class FeatureModule {}`,
        });
        const ts = result.readText('feature.module.ts');
        expect(ts).not.toContain('EditorModule');
        expect(ts).toContain('EditorComponent');
        expect(ts).toContain('@seniorsistemas/angular-components/editor');
    });
    // ─── DynamicForm: remoção de modules ─────────────────────────────────────
    it('deve remover propriedade modules de config { type: editor }', async () => {
        const result = await runMigration({
            'form.config.ts': `
const fields = [
    { type: 'editor', name: 'obs', modules: { toolbar: [['bold', 'italic']] } }
];`,
        });
        const ts = result.readText('form.config.ts');
        expect(ts).not.toContain('modules');
        expect(ts).toContain("type: 'editor'");
        expect(ts).toContain("name: 'obs'");
    });
    it('deve remover modules quando é a última propriedade do objeto editor', async () => {
        const result = await runMigration({
            'form.config.ts': `const f = { type: 'editor', name: 'x', modules: {} };`,
        });
        const ts = result.readText('form.config.ts');
        expect(ts).not.toContain('modules');
        expect(ts).toContain("type: 'editor'");
        expect(ts).toContain("name: 'x'");
    });
    it('deve remover modules quando é a única propriedade extra (além de type)', async () => {
        const result = await runMigration({
            'form.config.ts': `const f = { type: 'editor', modules: { toolbar: true } };`,
        });
        const ts = result.readText('form.config.ts');
        expect(ts).not.toContain('modules');
    });
    it('não deve modificar objeto sem type editor ao ter propriedade modules', async () => {
        const content = `const f = { type: 'text', name: 'x', modules: {} };`;
        const result = await runMigration({
            'form.config.ts': content,
        });
        expect(result.readText('form.config.ts')).toBe(content);
    });
    // ─── DynamicForm: migração de formats ────────────────────────────────────
    it('deve remover formato Quill sem equivalente e manter os suportados', async () => {
        const result = await runMigration({
            'form.config.ts': `const f = { type: 'editor', formats: ['bold', 'background', 'italic'] };`,
        });
        const ts = result.readText('form.config.ts');
        expect(ts).toContain("'bold'");
        expect(ts).toContain("'italic'");
        expect(ts).not.toContain("'background'");
    });
    it('deve mapear "bullet" para "list"', async () => {
        const result = await runMigration({
            'form.config.ts': `const f = { type: 'editor', formats: ['bullet', 'bold'] };`,
        });
        const ts = result.readText('form.config.ts');
        expect(ts).toContain("'list'");
        expect(ts).not.toContain("'bullet'");
        expect(ts).toContain("'bold'");
    });
    it('deve deduplicar quando "bullet" e "list" estão ambos presentes', async () => {
        const result = await runMigration({
            'form.config.ts': `const f = { type: 'editor', formats: ['list', 'bullet', 'bold'] };`,
        });
        const ts = result.readText('form.config.ts');
        const matches = ts.match(/'list'/g) ?? [];
        expect(matches.length).toBe(1);
        expect(ts).not.toContain("'bullet'");
    });
    it('deve remover todos os formatos não suportados', async () => {
        const result = await runMigration({
            'form.config.ts': `const f = { type: 'editor', formats: ['background', 'font', 'size', 'script'] };`,
        });
        const ts = result.readText('form.config.ts');
        expect(ts).toContain('formats: []');
    });
    it('não deve modificar formats com apenas valores suportados', async () => {
        const content = `const f = { type: 'editor', name: 'x', formats: ['bold', 'italic'] };`;
        const result = await runMigration({ 'form.config.ts': content });
        expect(result.readText('form.config.ts')).toBe(content);
    });
    it('deve manter os formats corretos após remoção: bold, italic, underline, link', async () => {
        const result = await runMigration({
            'form.config.ts': `const f = { type: 'editor', formats: ['bold', 'italic', 'underline', 'link', 'video'] };`,
        });
        const ts = result.readText('form.config.ts');
        expect(ts).toContain("'bold'");
        expect(ts).toContain("'italic'");
        expect(ts).toContain("'underline'");
        expect(ts).toContain("'link'");
        expect(ts).not.toContain("'video'");
    });
    // ─── DynamicForm: modules + formats juntos ────────────────────────────────
    it('deve remover modules e migrar formats no mesmo objeto', async () => {
        const result = await runMigration({
            'form.config.ts': `
const fields = [
    { type: 'editor', name: 'obs', formats: ['bold', 'background'], modules: { toolbar: true } }
];`,
        });
        const ts = result.readText('form.config.ts');
        expect(ts).not.toContain('modules');
        expect(ts).not.toContain("'background'");
        expect(ts).toContain("'bold'");
    });
    // ─── package.json ─────────────────────────────────────────────────────────
    it('deve remover quill de dependencies e adicionar @tiptap/*', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor></p-editor>`,
            '/package.json': JSON.stringify({
                dependencies: { quill: '^2.0.0', 'primeng': '^18.0.0' },
            }),
        });
        const pkg = JSON.parse(result.readText('/package.json'));
        expect(pkg.dependencies['quill']).toBeUndefined();
        expect(pkg.dependencies['@tiptap/core']).toBeDefined();
        expect(pkg.dependencies['@tiptap/starter-kit']).toBeDefined();
        expect(pkg.dependencies['@tiptap/extension-color']).toBeDefined();
    });
    it('deve remover quill de devDependencies', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor></p-editor>`,
            '/package.json': JSON.stringify({
                devDependencies: { quill: '^2.0.0' },
            }),
        });
        const pkg = JSON.parse(result.readText('/package.json'));
        expect(pkg.devDependencies['quill']).toBeUndefined();
    });
    it('não deve duplicar @tiptap/* já presentes no package.json', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor></p-editor>`,
            '/package.json': JSON.stringify({
                dependencies: { '@tiptap/core': '^3.27.0' },
            }),
        });
        const pkg = JSON.parse(result.readText('/package.json'));
        const coreEntries = Object.keys(pkg.dependencies).filter((k) => k === '@tiptap/core');
        expect(coreEntries.length).toBe(1);
        expect(pkg.dependencies['@tiptap/core']).toBe('^3.27.0');
    });
    it('deve adicionar todos os 12 pacotes @tiptap/*', async () => {
        const result = await runMigration({
            'app.component.html': `<p-editor></p-editor>`,
            '/package.json': JSON.stringify({ dependencies: {} }),
        });
        const pkg = JSON.parse(result.readText('/package.json'));
        const expected = [
            '@tiptap/core',
            '@tiptap/extension-color',
            '@tiptap/extension-highlight',
            '@tiptap/extension-image',
            '@tiptap/extension-placeholder',
            '@tiptap/extension-subscript',
            '@tiptap/extension-superscript',
            '@tiptap/extension-text-align',
            '@tiptap/extension-text-style',
            '@tiptap/extension-underline',
            '@tiptap/pm',
            '@tiptap/starter-kit',
        ];
        for (const name of expected) {
            expect(pkg.dependencies[name]).toBeDefined(`${name} deve estar em dependencies`);
        }
    });
});
//# sourceMappingURL=index.test.js.map