import { BooleanField } from '@dvn/core/doldurma/src/fields/boolean';
import { DataTransferField } from '@dvn/core/doldurma/src/fields/query-include/data-transfer-field';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ModifierTrimmer } from '@dvn/core/doldurma/src/interceptors/modifiers/ModifierTrimmer';
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 { dataConfigs } from '../../../../../mentornity-console/src/configs/data-configs';
import { FormHandlerUpdateDomain } from '../../handlers/form-handler-update-domain';
import { damgaSpace } from '../data-configs';
export const ID = 'update-domain-and-target-form';
const domainRegex = '(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]';
export const config = (apiDamga) => FormViewConfig.decode({
  _id: ID,
  base: 'devingen-dev',
  title: 'Update domain',
  space: damgaSpace,
  table: dataConfigs['damga-domains'],
  type: AppViewType.FormView,
  handler: new FormHandlerUpdateDomain(apiDamga, ID),
  ui: {
    fields: [
      // this field is passed by the parameters. this is a data holder field to pass the data to form handler
      {
        field: new DataTransferField('domainId'),
      },
      {
        field: new TextField('domain', 'Domain').disable()
          .require(),
      },
      {
        field: new TextField('target', 'Target', 'acme.co or 1.2.3.4:5678', 'Traffic will be forwarded to this address.').addInterceptor('onBlur', new ValidatorRegex(domainRegex, 'Not a valid domain or address.'))
          .addInterceptor('onSubmit', new ModifierTrimmer())
          .require(),
      },
      {
        field: new BooleanField('overrideHost', 'Override Host', 'Overrides the target host with domain while accessing the target.'),
      },
      {
        field: new BooleanField('useTLS', 'Use TLS', 'Uses **https** instead of **http** while accessing the target.'),
      }
    ],
  },
  params: {
    domainId: {
      required: true,
    },
    domain: {
      required: true,
    },
    target: {
      required: true,
    },
    overrideHost: {
      required: true,
    },
    useTLS: {
      required: true,
    },
  },
});
