import { cloneDeep } from 'lodash';
import { describe, it, expect } from 'vitest';

import type { PageDSL6 } from '../../../index.js';
import initialAppV6DSL from '../../../utils/pageFixtures/v6/initial_app.js';
import withNestedModal from '../../../utils/pageFixtures/v6/nested_modal.js';
import slideout_without_child from '../../../utils/pageFixtures/v6/slideout_without_child.js';
import with2ModalsAppV6DSL from '../../../utils/pageFixtures/v6/with_2_modals.js';
import withMainCanvasChildrenV6DSL from '../../../utils/pageFixtures/v6/with_main_canvas_children.js';
import withSlideoutsAppV6DSL from '../../../utils/pageFixtures/v6/with_slideouts.js';
import withWidgetsNamedPageV6DSL from '../../../utils/pageFixtures/v6/with_widgets_named_page.js';
import { migrate6to7 } from './v6to7.js';

describe('version 6 to version 7', () => {
  it(`returns a new object`, () => {
    const originalInput = cloneDeep(initialAppV6DSL);
    const migrated = migrate6to7(originalInput as PageDSL6);
    expect(migrated).not.toStrictEqual(originalInput);
  });

  it('should migrate an initial app DSL', () => {
    const migrated = migrate6to7(cloneDeep(initialAppV6DSL) as PageDSL6);
    expect(migrated).toMatchSnapshot('initial app');
  });

  it('should migrate an app DSL with containers at the top level', () => {
    const migrated = migrate6to7(cloneDeep(withSlideoutsAppV6DSL) as PageDSL6);
    expect(migrated).toMatchSnapshot('app with slideouts');
  });

  it('should migrate an app DSL with main canvas children', () => {
    const migrated = migrate6to7(cloneDeep(withMainCanvasChildrenV6DSL as PageDSL6));
    expect(migrated).toMatchSnapshot('app with main canvas children');
  });

  it('should migrate an app DSL with widgets that have the name "page"', () => {
    const migrated = migrate6to7(cloneDeep(withWidgetsNamedPageV6DSL as PageDSL6));
    expect(migrated).toMatchSnapshot('app with widgets named "page"');
  });

  it('should migrate an APP with two modals', () => {
    const migrated = migrate6to7(cloneDeep(with2ModalsAppV6DSL as PageDSL6));
    expect(migrated).toMatchSnapshot('app with two modals');
  });

  it('should migrate an APP with a nested modal', () => {
    const migrated = migrate6to7(cloneDeep(withNestedModal as PageDSL6));
    expect(migrated).toMatchSnapshot('app with nested modal');
  });

  it('should migrate an edge case', () => {
    const migrated = migrate6to7(cloneDeep(slideout_without_child as PageDSL6));
    expect(migrated.children?.[0].children?.[0].children?.[0].type).toBe('SLIDEOUT_WIDGET');
    expect(migrated.children?.[0].children?.[0].children?.[0].children?.[0]).toMatchObject({ type: 'SECTION_WIDGET' });
  });
});
