import {
  EditorLanguage,
  FormComponentType,
  FormSectionLayout,
  GRAPHQL_BASE_PLUGIN_ID,
  Plugin,
  PluginResponseType,
  PluginType
} from '../../types/index.js';
import { PUBLIC_INTEGRATIONS_LOGO_URL, DOCS_BASE_URL } from './constants.js';

export const GraphQLPluginVersions = {
  V1: '0.0.1',
  V8: '0.0.8',
  V9: '0.0.9',
  V10: '0.0.10'
};

export const GraphQLPlugin: Plugin = {
  id: GRAPHQL_BASE_PLUGIN_ID,
  name: 'GraphQL',
  moduleName: 'GraphQLPlugin',
  modulePath: 'plugins/graphql/GraphQLPlugin',
  iconLocation: `${PUBLIC_INTEGRATIONS_LOGO_URL}/graphql.png`,
  docsUrl: `${DOCS_BASE_URL}/graphql`,
  type: PluginType.API,
  responseType: PluginResponseType.JSON,
  hasRawRequest: true,
  rawRequestName: 'Executed request',
  datasourceTemplate: {
    sections: []
  },
  actionTemplate: {
    sections: [
      {
        name: 'main',
        items: [
          {
            label: 'URL',
            name: 'path',
            startVersion: GraphQLPluginVersions.V1,
            componentType: FormComponentType.DYNAMIC_INPUT_TEXT,
            placeholder: 'https://www.example.com/api?filter={{Dropdown1.selectedOptionValue}}',
            rules: [{ required: true, message: 'URL is required' }]
          }
        ]
      },
      {
        name: 'tabs',
        layout: FormSectionLayout.TABS,
        items: [
          {
            label: 'Query',
            name: 'body',
            startVersion: GraphQLPluginVersions.V1,
            componentType: FormComponentType.CODE_EDITOR,
            language: EditorLanguage.JSON
          },
          {
            label: 'Variables',
            name: 'custom.variables.value',
            startVersion: GraphQLPluginVersions.V1,
            componentType: FormComponentType.CODE_EDITOR,
            language: EditorLanguage.JSON
          },
          {
            label: 'Headers',
            name: 'headers',
            startVersion: GraphQLPluginVersions.V1,
            componentType: FormComponentType.FIELD_LIST,
            initialValue: [
              { key: 'Content-Type', value: 'application/json' },
              { key: '', value: '' }
            ]
          }
        ]
      },
      {
        name: 'additionalSettings',
        sectionHeader: 'Additional Settings',
        isCollapsible: true,
        defaultCollapsed: true,
        asTitle: false,
        items: [
          {
            name: 'verboseHttpOutput',
            label: 'Include full request response in step output',
            lightLabelText: true,
            startVersion: GraphQLPluginVersions.V9,
            componentType: FormComponentType.SWITCH,
            initialValue: false,
            showConfirm: false,
            renderIconFirst: true
          },
          {
            name: 'failOnGraphqlErrors',
            label: 'Fail when request includes errors',
            lightLabelText: true,
            startVersion: GraphQLPluginVersions.V10,
            componentType: FormComponentType.SWITCH,
            initialValue: false,
            showConfirm: false,
            renderIconFirst: true
          }
        ]
      }
    ]
  },
  demoData: {
    path: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
    body: `query {
  allFilms {
    films {
      title
    }
  }
}`
  }
};
