/* eslint-disable no-irregular-whitespace */
import React from 'react';
import { createStore, combineReducers } from 'redux';
import { Router } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import { Provider } from 'react-redux';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { render, screen } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import Theme from '../../../../components/Theme';
import { App as ContentManagerApp } from '..';
import cmReducers from '../../../../reducers';
import useModels from '../useModels';

jest.mock('../useModels', () =>
  jest.fn(() => {
    return {};
  })
);

jest.mock('@fortawesome/react-fontawesome', () => ({
  FontAwesomeIcon: () => null,
}));

jest.mock('../../NoContentType', () => () => <div>NoContentType</div>);

jest.mock('@strapi/helper-plugin', () => ({
  ...jest.requireActual('@strapi/helper-plugin'),
  useGuidedTour: jest.fn(() => ({
    startSection: jest.fn(),
  })),
}));

describe('Content manager | App | main', () => {
  beforeEach(() => {
    jest.resetModules(); // Most important - it clears the cache
  });

  it('should not crash', () => {
    const contentManagerState = {
      collectionTypeLinks: [
        {
          uid: 'category',
          title: 'Categories',
          name: 'category',
          to: '/content-manager/collectionType/category',
          kind: 'collectionType',
          isDisplayed: true,
          permissions: [
            {
              action: 'plugin::content-manager.explorer.read',
              subject: 'category',
            },
          ],
        },
      ],
      singleTypeLinks: [
        {
          uid: 'homepage',
          title: 'Home page',
          name: 'homepage',
          to: '/homepage',
          kind: 'singleType',
          isDisplayed: true,
          permissions: [
            {
              action: 'plugin::content-manager.explorer.read',
              subject: 'homepage',
            },
          ],
        },
      ],
      models: [
        {
          kind: 'collectionType',
          uid: 'category',
          isDisplayed: true,
          info: { label: 'Categories', name: 'category' },
        },
        {
          kind: 'singleType',
          isDisplayed: true,
          uid: 'homepage',
          info: { label: 'Home page', name: 'homepage' },
        },
      ],
      components: [],
      status: 'resolved',
    };
    useModels.mockImplementation(() => contentManagerState);
    const rootReducer = combineReducers(cmReducers);
    const store = createStore(rootReducer, { 'content-manager_app': contentManagerState });
    const history = createMemoryHistory();
    history.push('/content-manager');

    const { container } = render(
      <IntlProvider messages={{}} defaultLocale="en" locale="en">
        <Theme>
          <DndProvider backend={HTML5Backend}>
            <Provider store={store}>
              <Router history={history}>
                <ContentManagerApp />
              </Router>
            </Provider>
          </DndProvider>
        </Theme>
      </IntlProvider>
    );

    expect(screen.getByText('Home page')).toBeVisible();
    expect(screen.getByText('Categories')).toBeVisible();
    expect(history.location.pathname).toEqual('/content-manager/collectionType/category');
    expect(container.firstChild).toMatchInlineSnapshot(`
      .c31 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-flex-direction: row;
        -ms-flex-direction: row;
        flex-direction: row;
        -webkit-box-pack: space-around;
        -webkit-justify-content: space-around;
        -ms-flex-pack: space-around;
        justify-content: space-around;
        -webkit-align-items: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
      }

      .c33 {
        border: 0;
        -webkit-clip: rect(0 0 0 0);
        clip: rect(0 0 0 0);
        height: 1px;
        margin: -1px;
        overflow: hidden;
        padding: 0;
        position: absolute;
        width: 1px;
      }

      .c34 {
        -webkit-animation: gzYjWD 1s infinite linear;
        animation: gzYjWD 1s infinite linear;
      }

      .c32 {
        height: 100vh;
      }

      .c29 {
        padding-bottom: 56px;
      }

      .c0 {
        display: grid;
        grid-template-columns: auto 1fr;
      }

      .c30 {
        overflow-x: hidden;
      }

      .c2 {
        padding-top: 24px;
        padding-right: 16px;
        padding-bottom: 8px;
        padding-left: 24px;
      }

      .c8 {
        padding-top: 16px;
      }

      .c9 {
        background: #eaeaef;
      }

      .c12 {
        padding-top: 8px;
        padding-bottom: 16px;
      }

      .c14 {
        padding-top: 8px;
        padding-right: 16px;
        padding-bottom: 8px;
        padding-left: 24px;
      }

      .c19 {
        padding-right: 4px;
      }

      .c21 {
        background: #eaeaef;
        padding: 4px;
        border-radius: 4px;
        min-width: 20px;
      }

      .c24 {
        background: #f6f6f9;
        padding-top: 8px;
        padding-bottom: 8px;
        padding-left: 32px;
      }

      .c27 {
        padding-left: 8px;
      }

      .c1 {
        width: 14.5rem;
        background: #f6f6f9;
        position: -webkit-sticky;
        position: sticky;
        top: 0;
        height: 100vh;
        overflow-y: auto;
        border-right: 1px solid #dcdce4;
        z-index: 1;
      }

      .c3 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-flex-direction: row;
        -ms-flex-direction: row;
        flex-direction: row;
        -webkit-box-pack: justify;
        -webkit-justify-content: space-between;
        -ms-flex-pack: justify;
        justify-content: space-between;
        -webkit-align-items: flex-start;
        -webkit-box-align: flex-start;
        -ms-flex-align: flex-start;
        align-items: flex-start;
      }

      .c16 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-flex-direction: row;
        -ms-flex-direction: row;
        flex-direction: row;
        -webkit-box-pack: justify;
        -webkit-justify-content: space-between;
        -ms-flex-pack: justify;
        justify-content: space-between;
        -webkit-align-items: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
      }

      .c17 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-flex-direction: row;
        -ms-flex-direction: row;
        flex-direction: row;
        -webkit-align-items: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
      }

      .c22 {
        display: -webkit-inline-box;
        display: -webkit-inline-flex;
        display: -ms-inline-flexbox;
        display: inline-flex;
        -webkit-flex-direction: row;
        -ms-flex-direction: row;
        flex-direction: row;
        -webkit-box-pack: center;
        -webkit-justify-content: center;
        -ms-flex-pack: center;
        justify-content: center;
        -webkit-align-items: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
      }

      .c5 {
        color: #32324d;
        font-weight: 600;
        font-size: 1.125rem;
        line-height: 1.22;
      }

      .c20 {
        color: #666687;
        font-weight: 600;
        font-size: 0.6875rem;
        line-height: 1.45;
        text-transform: uppercase;
      }

      .c28 {
        color: #32324d;
        font-size: 0.875rem;
        line-height: 1.43;
      }

      .c6 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        cursor: pointer;
        padding: 8px;
        border-radius: 4px;
        background: #ffffff;
        border: 1px solid #dcdce4;
        position: relative;
        outline: none;
      }

      .c6 svg {
        height: 12px;
        width: 12px;
      }

      .c6 svg > g,
      .c6 svg path {
        fill: #ffffff;
      }

      .c6[aria-disabled='true'] {
        pointer-events: none;
      }

      .c6:after {
        -webkit-transition-property: all;
        transition-property: all;
        -webkit-transition-duration: 0.2s;
        transition-duration: 0.2s;
        border-radius: 8px;
        content: '';
        position: absolute;
        top: -4px;
        bottom: -4px;
        left: -4px;
        right: -4px;
        border: 2px solid transparent;
      }

      .c6:focus-visible {
        outline: none;
      }

      .c6:focus-visible:after {
        border-radius: 8px;
        content: '';
        position: absolute;
        top: -5px;
        bottom: -5px;
        left: -5px;
        right: -5px;
        border: 2px solid #4945ff;
      }

      .c7 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-align-items: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
        -webkit-box-pack: center;
        -webkit-justify-content: center;
        -ms-flex-pack: center;
        justify-content: center;
        height: 2rem;
        width: 2rem;
      }

      .c7 svg > g,
      .c7 svg path {
        fill: #8e8ea9;
      }

      .c7:hover svg > g,
      .c7:hover svg path {
        fill: #666687;
      }

      .c7:active svg > g,
      .c7:active svg path {
        fill: #a5a5ba;
      }

      .c7[aria-disabled='true'] {
        background-color: #eaeaef;
      }

      .c7[aria-disabled='true'] svg path {
        fill: #666687;
      }

      .c10 {
        height: 1px;
        border: none;
        margin: 0;
      }

      .c11 {
        width: 1.5rem;
        background-color: #dcdce4;
      }

      .c25 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-align-items: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
        -webkit-box-pack: justify;
        -webkit-justify-content: space-between;
        -ms-flex-pack: justify;
        justify-content: space-between;
        -webkit-text-decoration: none;
        text-decoration: none;
        color: #32324d;
      }

      .c25 svg > * {
        fill: #666687;
      }

      .c25.active {
        background-color: #f0f0ff;
        border-right: 2px solid #4945ff;
      }

      .c25.active svg > * {
        fill: #271fe0;
      }

      .c25.active .c4 {
        color: #271fe0;
        font-weight: 500;
      }

      .c25:focus-visible {
        outline-offset: -2px;
      }

      .c26 {
        width: 0.75rem;
        height: 0.25rem;
      }

      .c26 * {
        fill: #666687;
      }

      .c18 {
        border: none;
        padding: 0;
        background: transparent;
      }

      .c15 {
        max-height: 2rem;
      }

      .c15 svg {
        height: 0.25rem;
      }

      .c15 svg path {
        fill: #8e8ea9;
      }

      .c23 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-align-items: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
      }

      .c13 {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-flex-direction: column;
        -ms-flex-direction: column;
        flex-direction: column;
      }

      .c13 > * {
        margin-top: 0;
        margin-bottom: 0;
      }

      .c13 > * + * {
        margin-top: 8px;
      }

      <div
        class="c0"
      >
        <nav
          aria-label="Content"
          class="c1"
        >
          <div
            class="c2"
          >
            <div
              class="c3"
            >
              <h2
                class="c4 c5"
              >
                Content
              </h2>
              <span>
                <button
                  aria-disabled="false"
                  aria-labelledby="tooltip-2"
                  class="c6 c7"
                  tabindex="0"
                  type="button"
                >
                  <svg
                    fill="none"
                    height="1em"
                    viewBox="0 0 24 24"
                    width="1em"
                    xmlns="http://www.w3.org/2000/svg"
                  >
                    <path
                      clip-rule="evenodd"
                      d="M23.813 20.163l-5.3-5.367a9.792 9.792 0 001.312-4.867C19.825 4.455 15.375 0 9.913 0 4.45 0 0 4.455 0 9.929c0 5.473 4.45 9.928 9.912 9.928a9.757 9.757 0 005.007-1.4l5.275 5.35a.634.634 0 00.913 0l2.706-2.737a.641.641 0 000-.907zM9.91 3.867c3.338 0 6.05 2.718 6.05 6.061s-2.712 6.061-6.05 6.061c-3.337 0-6.05-2.718-6.05-6.06 0-3.344 2.713-6.062 6.05-6.062z"
                      fill="#32324D"
                      fill-rule="evenodd"
                    />
                  </svg>
                </button>
              </span>
            </div>
            <div
              class="c8"
            >
              <hr
                class="c9 c10 c11"
              />
            </div>
          </div>
          <div
            class="c12"
          >
            <ul
              class="c13"
            >
              <li>
                <div
                  class="c14 c15"
                >
                  <div
                    class="c16"
                  >
                    <div
                      class="c17 c18"
                    >
                      <div
                        class="c19"
                      >
                        <span
                          class="c4 c20"
                        >
                          Collection Types
                        </span>
                      </div>
                    </div>
                    <div
                      class="c21 c22 c23"
                    >
                      <span
                        class="c4 c20"
                      >
                        1
                      </span>
                    </div>
                  </div>
                </div>
                <ul
                  id="subnav-list-4"
                >
                  <li>
                    <a
                      aria-current="page"
                      class="c24 c25 active"
                      href="/content-manager/collectionType/category"
                    >
                      <div
                        class="c17"
                      >
                        <svg
                          class="c26"
                          fill="none"
                          height="1em"
                          viewBox="0 0 4 4"
                          width="1em"
                          xmlns="http://www.w3.org/2000/svg"
                        >
                          <rect
                            fill="#A5A5BA"
                            height="4"
                            rx="2"
                            width="4"
                          />
                        </svg>
                        <div
                          class="c27"
                        >
                          <span
                            class="c4 c28"
                          >
                            Categories
                          </span>
                        </div>
                      </div>
                    </a>
                  </li>
                </ul>
              </li>
              <li>
                <div
                  class="c14 c15"
                >
                  <div
                    class="c16"
                  >
                    <div
                      class="c17 c18"
                    >
                      <div
                        class="c19"
                      >
                        <span
                          class="c4 c20"
                        >
                          Single Types
                        </span>
                      </div>
                    </div>
                    <div
                      class="c21 c22 c23"
                    >
                      <span
                        class="c4 c20"
                      >
                        1
                      </span>
                    </div>
                  </div>
                </div>
                <ul
                  id="subnav-list-5"
                >
                  <li>
                    <a
                      class="c24 c25"
                      href="/homepage"
                    >
                      <div
                        class="c17"
                      >
                        <svg
                          class="c26"
                          fill="none"
                          height="1em"
                          viewBox="0 0 4 4"
                          width="1em"
                          xmlns="http://www.w3.org/2000/svg"
                        >
                          <rect
                            fill="#A5A5BA"
                            height="4"
                            rx="2"
                            width="4"
                          />
                        </svg>
                        <div
                          class="c27"
                        >
                          <span
                            class="c4 c28"
                          >
                            Home page
                          </span>
                        </div>
                      </div>
                    </a>
                  </li>
                </ul>
              </li>
            </ul>
          </div>
        </nav>
        <div
          class="c29 c30"
        >
          <div
            class="c31 c32"
            data-testid="loader"
          >
            <div
              aria-live="assertive"
              role="alert"
            >
              <div
                class="c33"
              >
                Loading content.
              </div>
              <img
                aria-hidden="true"
                class="c34"
                src="data:image/svg+xml;base64, "
              />
            </div>
          </div>
        </div>
      </div>
    `);
  });

  it('should redirect to the single type', () => {
    const contentManagerState = {
      collectionTypeLinks: [],
      singleTypeLinks: [
        {
          uid: 'homepage',
          title: 'Home page',
          name: 'homepage',
          to: '/content-manager/homepage',
          kind: 'singleType',
          isDisplayed: true,
          permissions: [
            {
              action: 'plugin::content-manager.explorer.read',
              subject: 'homepage',
            },
          ],
        },
      ],
      models: [
        {
          kind: 'collectionType',
          uid: 'category',
          info: { label: 'Categories', name: 'category' },
          isDisplayed: true,
        },
        {
          kind: 'singleType',
          isDisplayed: true,
          uid: 'homepage',
          info: { label: 'Home page', name: 'homepage' },
        },
      ],
      components: [],
      status: 'resolved',
    };
    useModels.mockImplementation(() => contentManagerState);
    const rootReducer = combineReducers(cmReducers);
    const store = createStore(rootReducer, { 'content-manager_app': contentManagerState });
    const history = createMemoryHistory();
    history.push('/content-manager');

    render(
      <IntlProvider messages={{}} defaultLocale="en" locale="en">
        <Theme>
          <DndProvider backend={HTML5Backend}>
            <Provider store={store}>
              <Router history={history}>
                <ContentManagerApp />
              </Router>
            </Provider>
          </DndProvider>
        </Theme>
      </IntlProvider>
    );

    expect(history.location.pathname).toEqual('/content-manager/homepage');
  });

  it('should redirect to 403 page', () => {
    const history = createMemoryHistory();
    const contentManagerState = {
      collectionTypeLinks: [],
      singleTypeLinks: [],
      models: [
        {
          kind: 'collectionType',
          uid: 'category',
          isDisplayed: true,
          info: { label: 'Categories', name: 'category' },
        },
        { kind: 'singleType', uid: 'homepage', info: { label: 'Home page', name: 'homepage' } },
      ],
      components: [],
      status: 'resolved',
    };
    useModels.mockImplementation(() => contentManagerState);
    jest.mock('../useModels', () =>
      jest.fn(() => {
        return contentManagerState;
      })
    );
    const rootReducer = combineReducers(cmReducers);
    const store = createStore(rootReducer, { 'content-manager_app': contentManagerState });
    history.push('/content-manager/collectionType/category');

    render(
      <IntlProvider messages={{}} defaultLocale="en" locale="en">
        <Theme>
          <DndProvider backend={HTML5Backend}>
            <Provider store={store}>
              <Router history={history}>
                <ContentManagerApp />
              </Router>
            </Provider>
          </DndProvider>
        </Theme>
      </IntlProvider>
    );

    expect(history.location.pathname).toEqual('/content-manager/403');
  });

  it('should redirect to the no-content-types page', () => {
    const history = createMemoryHistory();
    const contentManagerState = {
      collectionTypeLinks: [],
      singleTypeLinks: [],
      models: [
        {
          kind: 'collectionType',
          uid: 'category',
          info: { label: 'Categories', name: 'category' },
          isDisplayed: false,
        },
      ],
      components: [],
      status: 'resolved',
    };
    useModels.mockImplementation(() => contentManagerState);
    jest.mock('../useModels', () =>
      jest.fn(() => {
        return contentManagerState;
      })
    );
    const rootReducer = combineReducers(cmReducers);
    const store = createStore(rootReducer, { 'content-manager_app': contentManagerState });
    history.push('/content-manager/collectionType/category');

    render(
      <IntlProvider messages={{}} defaultLocale="en" locale="en">
        <Theme>
          <DndProvider backend={HTML5Backend}>
            <Provider store={store}>
              <Router history={history}>
                <ContentManagerApp />
              </Router>
            </Provider>
          </DndProvider>
        </Theme>
      </IntlProvider>
    );

    expect(history.location.pathname).toEqual('/content-manager/no-content-types');
  });
});
