import { randomInt } from 'crypto';
import { ComponentSetWebModelForCreate } from '@squiz/component-lib';
import { ContentApi } from '@squiz/component-web-api-lib';

export const COMPONENT_SET_WEB_PATH = 'local-development-only' + randomInt(10000);
export const COMPONENT_NAME = 'test-page-render';
export const COMPONENT_NAMESPACE = 'unit-test-components';
export const COMPONENT_CONTENT_ID = 'component-content-id' + randomInt(10000);
export const COMPONENT_NODE_ID = `${COMPONENT_NAMESPACE}/${COMPONENT_NAME}/1.0.0/main`;

//The componentSet for the component in the content item
export const componentSet: ComponentSetWebModelForCreate = {
  webPath: COMPONENT_SET_WEB_PATH,
  displayName: 'test-set',
  description: 'test-set',
  headers: {},
  components: {},
  componentVersionRules: {
    [`${COMPONENT_NAMESPACE}/${COMPONENT_NAME}`]: {
      renderableVersionPattern: '1.0.0',
      editableVersions: [],
      excludedVersions: [],
    },
  },
};

// The content item for the component
export const componentContentItem = {
  id: COMPONENT_CONTENT_ID,
  schemaName: COMPONENT_NODE_ID,
  content: {
    text: 'component-input-string',
  },
};

// The page content item which references the content item
export const pageContents: ContentApi.PageContentsCreate = {
  name: 'page-contents-test-1',
  layouts: [
    {
      name: 'single-column',
      content: {
        main: [
          {
            type: 'tag',
            tag: 'h1',
            children: [
              {
                type: 'text',
                value: 'Hello World!',
              },
            ],
          },
          {
            type: 'component',
            componentSet: COMPONENT_SET_WEB_PATH,
            componentId: COMPONENT_NODE_ID,
            contentItem: { id: COMPONENT_CONTENT_ID },
          },
          {
            type: 'tag',
            tag: 'p',
            children: [
              {
                type: 'text',
                value: 'Bottom text',
              },
            ],
          },
        ],
      },
    },
  ],
};

export const pageContentsWithVariants: ContentApi.PageContentsCreate = {
  name: 'page-contents-test-2',
  layouts: [
    {
      name: 'single-column',
      content: {
        main: [
          {
            type: 'variation',
            default: {
              node: {
                type: 'tag',
                tag: 'h1',
                children: [
                  {
                    type: 'text',
                    value: 'Hello World!',
                  },
                ],
              },
            },
            variants: [
              {
                node: {
                  type: 'tag',
                  tag: 'h1',
                  children: [
                    {
                      type: 'text',
                      value: 'Hello Authenticated User with experiment B!',
                    },
                  ],
                },
                conditions: {
                  segment: { code: 'is-authenticated' },
                  experiment: { id: 'experiment1', variant: 'B' },
                },
              },
              {
                node: {
                  type: 'tag',
                  tag: 'h1',
                  children: [
                    {
                      type: 'text',
                      value: 'Hello Authenticated User!',
                    },
                  ],
                },
                conditions: {
                  segment: { code: 'is-authenticated' },
                },
              },
              {
                node: {
                  type: 'tag',
                  tag: 'h1',
                  children: [
                    {
                      type: 'text',
                      value: 'Hello Unauthenticated User!',
                    },
                  ],
                },
                conditions: {
                  segment: { code: 'is-unauthenticated' },
                },
              },
            ],
          },
        ],
      },
    },
  ],
};
