import { DvnDataAdapter } from '@dvn/core/adapters/DvnDataAdapter';
import { SingleChoiceField } from '@dvn/core/doldurma/src/fields/single-choice';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ModifierRegexCharRemover } from '@dvn/core/doldurma/src/interceptors/modifiers/ModifierRegexCharRemover';
import { ModifierTrimmer } from '@dvn/core/doldurma/src/interceptors/modifiers/ModifierTrimmer';
import { ValidatorLength } from '@dvn/core/doldurma/src/interceptors/validators/ValidatorLength';
import { ValidatorRegex } from '@dvn/core/doldurma/src/interceptors/validators/ValidatorRegex';
import { FormViewConfig } from '@dvn/core/models/form-view-config';
import { AppViewType } from '@dvn/core/models/view-config';
import { DvnDataProvider } from '@dvn/core/providers/DvnDataProvider';
import { DvnDataProviderTimeZone } from '@dvn/core/providers/time-zone-provider/DvnDataProviderTimeZone';
import { CreateOrganisationFormHandler } from '../../handlers/CreateOrganisationFormHandler';
import { dataConfigs } from '../data-configs';
import { userDataProvider } from '../data-provider-configs';
import { spaces } from '../spaces';
export const ID = 'form-view-organisation-create';
export const config = (dataApi) => FormViewConfig.decode({
  _id: ID,
  base: 'mentornity',
  title: 'Create Organisation',
  space: spaces['mentornity'],
  table: dataConfigs['organisations'],
  type: AppViewType.FormView,
  handler: new CreateOrganisationFormHandler(),
  ui: {
    fields: [
      {
        field: new SingleChoiceField('sourceOrganisationId', 'Program to clone', undefined, 'Search for a template or another program to clone.', 'select').setDataProvider(new DvnDataProvider(dataApi, 'organisations', {
          fields: [
            new TextField('domain', 'Domain'),
            new TextField('title', 'Title'),
          ],
          sort: [{ id: 'title', order: 1 }]
        }, undefined, {
          fields: [
            { fieldId: 'domain' },
            { fieldId: 'title' },
          ]
        })).setDataAdapter(new DvnDataAdapter('title', '_id'))
          .require(),
      },
      {
        field: new SingleChoiceField('admin', 'Admin', undefined, 'Search and select admins.', 'select').setDataProvider(userDataProvider(dataApi))
          .setDataAdapter(new DvnDataAdapter('firstName+lastName', '_id'))
          .require(),
      },
      {
        field: new SingleChoiceField('timeZone', 'Time Zone', undefined, 'Time zone of the program. Original program\'s time zone is used if empty.', 'select').setDataProvider(new DvnDataProviderTimeZone())
          .setDataAdapter(new DvnDataAdapter('label', 'value')),
      },
      {
        field: new TextField('newOrganisationTitle', 'Program name', '', 'Shown to users in the UI, emails and notifications.').addInterceptor('onBlur', new ValidatorLength(2, 100))
          .addInterceptor('onSubmit', new ModifierTrimmer())
          .require(),
      },
      {
        field: new TextField('newOrganisationDomain', 'Program domain', '', 'Used in the url and visible in browser address bar.').addInterceptor('onBlur', new ModifierRegexCharRemover(' '))
          .addInterceptor('onBlur', new ValidatorRegex('[a-z0-9\-]'))
          .addInterceptor('onBlur', new ValidatorLength(2, 100))
          .addInterceptor('onSubmit', new ModifierTrimmer())
          .require(),
      },
    ],
  }
});
