'use strict';

const cloneDeep = require('lodash/cloneDeep');
const merge = require('lodash/merge');

// Helpers.
const { createTestBuilder } = require('../../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../../test/helpers/strapi');
const form = require('../../../../../../test/helpers/generators');
const { createAuthRequest } = require('../../../../../../test/helpers/request');

const builder = createTestBuilder();
let strapi;
let rq;

const restart = async () => {
  await strapi.destroy();
  strapi = await createStrapiInstance();
  rq = await createAuthRequest({ strapi });
};

const transformLayout = (layout, partialNewLayout) => {
  return partialNewLayout.reduce((acc, row, rowIndex) => {
    acc[rowIndex] = row.map((column, columnIndex) => merge(acc[rowIndex][columnIndex], column));

    return acc;
  }, layout);
};

const FIXTURE_DEFAULT_LAYOUT = [
  [
    {
      name: 'title',
      size: 6,
    },
    {
      name: 'date',
      size: 4,
    },
  ],
  [
    {
      name: 'jsonField',
      size: 12,
    },
  ],
  [
    {
      name: 'content',
      size: 12,
    },
  ],
];

describe('Content Manager - Update Layout', () => {
  beforeAll(async () => {
    await builder.addContentTypes([form.article]).build();

    strapi = await createStrapiInstance();
    rq = await createAuthRequest({ strapi });
  });

  afterAll(async () => {
    await strapi.destroy();
    await builder.cleanup();
  });

  test('Fetch default layout', async () => {
    const { body } = await rq({
      url: '/content-manager/content-types/api::article.article/configuration',
      method: 'GET',
    });

    expect(body.data.contentType.layouts.edit).toStrictEqual(FIXTURE_DEFAULT_LAYOUT);
  });

  test('Update field size', async () => {
    const transformation = [
      [
        {
          name: 'title',
          size: 8,
        },

        {
          name: 'date',
          size: 4,
        },
      ],
    ];
    const payload = transformLayout(cloneDeep(FIXTURE_DEFAULT_LAYOUT), transformation);

    await rq({
      url: '/content-manager/content-types/api::article.article/configuration',
      method: 'PUT',
      body: {
        layouts: {
          edit: payload,
          editRelations: [],
          list: [],
        },
      },
    });

    const { body } = await rq({
      url: '/content-manager/content-types/api::article.article/configuration',
      method: 'GET',
    });

    const expectation = transformLayout(cloneDeep(FIXTURE_DEFAULT_LAYOUT), transformation);

    expect(body.data.contentType.layouts.edit).toStrictEqual(expectation);
  });

  test('Update field size with server restart and invalid JSON size', async () => {
    const transformation = [
      [
        {
          name: 'title',
          size: 8,
        },

        {
          name: 'date',
          size: 4,
        },
      ],

      [
        {
          name: 'jsonField',
          size: 6,
        },
      ],
    ];
    const payload = transformLayout(cloneDeep(FIXTURE_DEFAULT_LAYOUT), transformation);

    await rq({
      url: '/content-manager/content-types/api::article.article/configuration',
      method: 'PUT',
      body: {
        layouts: {
          edit: payload,
          editRelations: [],
          list: [],
        },
      },
    });

    await restart();

    const { body } = await rq({
      url: '/content-manager/content-types/api::article.article/configuration',
      method: 'GET',
    });

    const expectation = [
      [
        {
          name: 'title',
          size: 8,
        },

        {
          name: 'date',
          size: 4,
        },
      ],

      [
        {
          name: 'content',
          size: 12,
        },
      ],

      [
        {
          name: 'jsonField',
          size: 12,
        },
      ],
    ];

    expect(body.data.contentType.layouts.edit).toStrictEqual(expectation);
  });

  test('Update field size with server restart and invalid date size', async () => {
    const transformation = [
      [
        {
          name: 'title',
          size: 12,
        },

        {
          name: 'date',
          size: 14,
        },
      ],
    ];
    const payload = transformLayout(cloneDeep(FIXTURE_DEFAULT_LAYOUT), transformation);

    await rq({
      url: '/content-manager/content-types/api::article.article/configuration',
      method: 'PUT',
      body: {
        layouts: {
          edit: payload,
          editRelations: [],
          list: [],
        },
      },
    });

    await restart();

    const { body } = await rq({
      url: '/content-manager/content-types/api::article.article/configuration',
      method: 'GET',
    });

    const expectation = [
      [
        {
          name: 'title',
          size: 12,
        },
      ],
      [
        {
          name: 'jsonField',
          size: 12,
        },
      ],
      [
        {
          name: 'content',
          size: 12,
        },
      ],

      [
        {
          name: 'date',
          size: 4,
        },
      ],
    ];

    expect(body.data.contentType.layouts.edit).toStrictEqual(expectation);
  });
});

@@ -12,55 +12,8 @@
 ';%0A%0A
-const cloneDeep = require('lodash/cloneDeep');%0A
 cons
@@ -47,16 +47,16 @@
 erge');%0A
+
 %0A// Help
@@ -580,255 +580,8 @@
 %7D;%0A%0A
-const transformLayout = (layout, partialNewLayout) =%3E %7B%0A  return partialNewLayout.reduce((acc, row, rowIndex) =%3E %7B%0A    acc%5BrowIndex%5D = row.map((column, columnIndex) =%3E merge(acc%5BrowIndex%5D%5BcolumnIndex%5D, column));%0A%0A    return acc;%0A  %7D, layout);%0A%7D;%0A%0A
 cons
@@ -1695,34 +1695,18 @@
 d = 
-transformLayout(cloneDeep(
+merge(%5B%5D, 
 FIXT
@@ -1715,33 +1715,32 @@
 E_DEFAULT_LAYOUT
-)
 , transformation
@@ -2165,34 +2165,18 @@
 n = 
-transformLayout(cloneDeep(
+merge(%5B%5D, 
 FIXT
@@ -2185,33 +2185,32 @@
 E_DEFAULT_LAYOUT
-)
 , transformation
@@ -2673,34 +2673,18 @@
 d = 
-transformLayout(cloneDeep(
+merge(%5B%5D, 
 FIXT
@@ -2693,33 +2693,32 @@
 E_DEFAULT_LAYOUT
-)
 , transformation
@@ -3867,34 +3867,18 @@
 d = 
-transformLayout(cloneDeep(
+merge(%5B%5D, 
 FIXT
@@ -3895,17 +3895,16 @@
 T_LAYOUT
-)
 , transf

Refactor test to utilize lodash.merge