'use strict';

const index = require('./index-16c91445.js');
const InterceptorHandler = require('./InterceptorHandler-09a1e84b.js');
const dataModel = require('./data-model-f9df493f.js');
const BaseField = require('./BaseField-f50bb8f0.js');
const FieldModelGenerator = require('./FieldModelGenerator-61c1111e.js');
const date = require('./date-8e8ca89a.js');
const index$1 = require('./index-21b225d5.js');
const tableConfig = require('./table-config-d70a1046.js');
const membershipConfig = require('./membership-config-79450727.js');
const viewConfig = require('./view-config-1ef57be5.js');
const attachment = require('./attachment-c2bc9126.js');
const spaces = require('./spaces-ad9dfe8b.js');
require('./index-6923d308.js');

class RawDataField extends BaseField.BaseField {
  constructor(id, title, description, dataType = 'json') {
    super(id, 'rawData');
    this.dataType = 'json';
    this.id = id;
    this.title = title;
    this.description = description;
    this.dataType = dataType;
  }
  showPreview() {
    this.preview = true;
    return this;
  }
  getDisplayValue(value) {
    return value;
  }
  setDataType(dataType) {
    this.dataType = dataType;
    return this;
  }
}
RawDataField.type = 'rawData';

class RelationReferenceField extends BaseField.BaseField {
  constructor(id, title, titleField, imageField, relationCollection, nameInRelationCollection, otherCollection, nameOfOtherCollectionInRelationCollection, fields, otherCollectionFields) {
    super(id, 'relation-reference');
    this.title = title;
    this.titleField = titleField;
    this.imageField = imageField;
    this.relationCollection = relationCollection;
    this.nameInRelationCollection = nameInRelationCollection;
    this.otherCollection = otherCollection;
    this.nameOfOtherCollectionInRelationCollection = nameOfOtherCollectionInRelationCollection;
    this.fields = fields;
    this.otherCollectionFields = otherCollectionFields;
  }
  showPreview() {
    this.preview = true;
    return this;
  }
  getDisplayValue(value) {
    return value;
  }
}
RelationReferenceField.type = 'relation-reference';

const dataConfigs = {
  'auth': dataModel.DataModel.decode({
    _id: 'auth',
    _created: new Date(),
    _updated: new Date(),
    base: 'mentornity',
    fields: [
      new date.DateField('createdAt', 'Created At')
        .setDataType(date.DateDataType.Unix)
        .setDateFormat('DD MMM YYYY HH:mm'),
      new RawDataField('google', 'Google'),
      new RawDataField('microsoft', 'Microsoft'),
      new RawDataField('zoom', 'Zoom'),
    ],
    name: 'auth',
    space: spaces.spaces['mentornity'],
  }),
  'formResponses': dataModel.DataModel.decode({
    _id: 'formResponses',
    _created: new Date(),
    _updated: new Date(),
    base: 'mentornity',
    fields: [
      new date.DateField('createdAt', 'Created At')
        .setDataType(date.DateDataType.Unix)
        .setDateFormat('DD MMM YYYY HH:mm'),
      new RawDataField('data', 'Data'),
      new index.TextField('type', 'type'),
    ],
    name: 'formResponses',
    space: spaces.spaces['mentornity'],
  }),
  'configs': dataModel.DataModel.decode({
    _id: 'configs',
    _created: new Date(),
    _updated: new Date(),
    base: 'mentornity',
    fields: [
      new date.DateField('createdAt', 'Created At').setDataType(date.DateDataType.Unix).setDateFormat('DD MMM YYYY'),
    ],
    name: 'configs',
    space: spaces.spaces['mentornity'],
  }),
  'organisations': dataModel.DataModel.decode({
    _id: 'organisations',
    _created: new Date(),
    _updated: new Date(),
    base: 'mentornity',
    fields: [
      new date.DateField('createdAt', 'Created At')
        .setDataType(date.DateDataType.Unix)
        .setDateFormat('DD MMM YYYY'),
      new attachment.AttachmentField('coverUrl', ''),
      new attachment.AttachmentField('logoUrl', ''),
      new index.TextField('domain', 'Domain'),
      new index.TextField('title', 'Title'),
      new index.TextField('description', 'Description'),
      new index.TextField('webSiteUrl', 'Web Site').setInputType('url'),
      new index.TextField('facebookUrl', 'Facebook').setInputType('url'),
      new index.TextField('twitterUrl', 'Twitter').setInputType('url'),
      new index.TextField('linkedInUrl', 'LinkedIn').setInputType('url'),
      new index$1.SingleChoiceField('status', 'Status', [
        new index$1.SingleChoiceFieldOption('Trial', 'trial', 'warning'),
        new index$1.SingleChoiceFieldOption('Free', 'free', 'danger'),
        new index$1.SingleChoiceFieldOption('Paying', 'paying', 'primary'),
        new index$1.SingleChoiceFieldOption('Archived', 'archived', 'neutral'),
        new index$1.SingleChoiceFieldOption('Template', 'template', 'neutral'),
        new index$1.SingleChoiceFieldOption('Test', 'test', 'secondary'),
      ]),
      new index$1.SingleChoiceField('model', 'Model', [
        new index$1.SingleChoiceFieldOption('Structured Paired', 'structured-paired'),
        new index$1.SingleChoiceFieldOption('Structured Selective', 'structured-selective'),
        new index$1.SingleChoiceFieldOption('Freestyle Paired', 'freestyle-paired'),
        new index$1.SingleChoiceFieldOption('Freestyle Selective', 'freestyle-selective'),
        new index$1.SingleChoiceFieldOption('Pool', 'pool'),
        new index$1.SingleChoiceFieldOption('Group Pool', 'group-pool'),
        new index$1.SingleChoiceFieldOption('Group Paired', 'group-paired'),
        new index$1.SingleChoiceFieldOption('Startup Pool', 'startup-pool'),
        new index$1.SingleChoiceFieldOption('Startup Paired', 'startup-paired'),
        new index$1.SingleChoiceFieldOption('Structured Paired-Coaching', 'structured-paired-coaching'),
        new index$1.SingleChoiceFieldOption('Structured Selective-Coaching', 'structured-selective-coaching'),
      ]),
      {
        type: 'reverse-reference',
        id: 'summary',
        otherCollection: 'organisationSummaries',
        nameInOtherCollection: 'organisation',
        isSingle: true,
        fields: [{ id: 'metrics' }]
      }
    ],
    name: 'organisations',
    space: spaces.spaces['mentornity'],
  }),
  'scheduledjobs': dataModel.DataModel.decode({
    _id: 'scheduledjobs',
    _created: new Date(),
    _updated: new Date(),
    base: 'mentornity',
    fields: [
      new index.TextField('name', 'Name'),
      new date.DateField('scheduledAt', 'Scheduled At').setDateFormat('DD MMM YYYY HH:mm'),
      new date.DateField('createdAt', 'Created At')
        .setDataType(date.DateDataType.Unix)
        .setDateFormat('DD MMM YYYY HH:mm'),
      new index$1.SingleChoiceField('status', 'Status', [
        new index$1.SingleChoiceFieldOption('Ready', 'ready', 'neutral'),
        new index$1.SingleChoiceFieldOption('Executing', 'executing', 'primary'),
        new index$1.SingleChoiceFieldOption('Done', 'done', 'secondary'),
        new index$1.SingleChoiceFieldOption('Failed', 'failed', 'danger'),
        new index$1.SingleChoiceFieldOption('Crashed', 'crashed', 'danger'),
        new index$1.SingleChoiceFieldOption('Canceled', 'crashed', 'warning'),
      ]),
      new RawDataField('metadata', 'Metadata'),
      new RawDataField('output', 'Output'),
      new RawDataField('parameters', 'Parameters'),
    ],
    name: 'scheduledjobs',
    space: spaces.spaces['mentornity'],
  }),
  'sessions': membershipConfig.MembershipConfig.decode({
    _id: 'sessions',
    _created: new Date(),
    _updated: new Date(),
    agreements: [],
    base: 'mentornity',
    fields: [
      new date.DateField('createdAt', 'Initiated')
        .setDataType(date.DateDataType.Unix)
        .setDateFormat('DD MMM HH:mm'),
      new date.DateField('updatedAt', 'Last activity')
        .setDataType(date.DateDataType.Unix)
        .setDateFormat('DD MMM HH:mm'),
      new index.TextField('failureReason', 'Failure Reason'),
      new index$1.SingleChoiceField('status', 'Status', [
        new index$1.SingleChoiceFieldOption('Successful', 'successful', 'secondary'),
        new index$1.SingleChoiceFieldOption('Failed', 'failed', 'danger'),
        new index$1.SingleChoiceFieldOption('Logged Out', 'loggedOut', 'neutral'),
      ]),
      new FieldModelGenerator.ReferenceField('user', 'User', 'firstName', 'imageUrl', 'users', [
        new attachment.AttachmentField('imageUrl', ''),
        new index.TextField('firstName', 'First Name'),
      ]),
      new index$1.SingleChoiceField('client', 'Application', [
        new index$1.SingleChoiceFieldOption('Web Admin', 'web-admin'),
        new index$1.SingleChoiceFieldOption('Web Client', 'web-client'),
        new index$1.SingleChoiceFieldOption('Web Console', 'web-console'),
      ]),
    ],
    name: 'sessions',
    space: spaces.spaces['mentornity'],
  }),
  'users': membershipConfig.MembershipConfig.decode({
    _id: 'users',
    _created: new Date(),
    _updated: new Date(),
    agreements: [],
    base: 'mentornity',
    fields: [
      new date.DateField('createdAt', 'Date')
        .setDataType(date.DateDataType.Unix)
        .setDateFormat('DD MMM YYYY'),
      new attachment.AttachmentField('imageUrl', '').setShape('circle'),
      new index.TextField('email', 'Email'),
      new index.TextField('firstName', 'First Name'),
      new index.TextField('lastName', 'Last Name'),
      new index$1.SingleChoiceField('status', 'Status', [
        new index$1.SingleChoiceFieldOption('Active', 'active', 'secondary'),
        new index$1.SingleChoiceFieldOption('Invited', 'invited', 'warning'),
        new index$1.SingleChoiceFieldOption('Passive', 'notInvited', 'danger'),
      ]),
      new RelationReferenceField('memberships', 'Programs', 'title', 'logoUrl', 'memberships', 'user', 'organisations', 'organisation', [
        new index.TextField('status', '')
      ], [
        new index.TextField('logoUrl', ''),
        new index.TextField('title', ''),
      ]),
      {
        type: 'reverse-reference',
        id: 'lastSession',
        otherCollection: 'sessions',
        nameInOtherCollection: 'user',
        isSingle: true,
        fields: [{ id: 'updatedAt' }, { id: 'createdAt' }],
        limit: 1,
        sort: {
          order: -1,
          id: 'updatedAt'
        }
      },
    ],
    name: 'users',
    space: spaces.spaces['mentornity'],
  }),
  'whiteLabels': dataModel.DataModel.decode({
    _id: 'whiteLabels',
    _created: new Date(),
    _updated: new Date(),
    base: 'mentornity',
    fields: [
      new date.DateField('createdAt', 'Created At').setDataType(date.DateDataType.Unix).setDateFormat('DD MMM YYYY'),
      new date.DateField('updatedAt', 'Created At').setDataType(date.DateDataType.Unix).setDateFormat('DD MMM YYYY'),
      new index.TextField('domain', 'Domain').require(),
      new index.TextField('title', 'Title').require(),
      new index.TextField('description', 'Description').require(),
      new index.TextField('senderEmail', '').require(),
      new index.TextField('supportEmail', '').require(),
      new index.TextField('mailIconUrl', '').require(),
      new index.TextField('landingPageUrl', ''),
      new index.TextField('facebookUrl', ''),
      new index.TextField('twitterUrl', ''),
      new index.TextField('linkedInUrl', ''),
      new index.TextField('webSiteUrl', ''),
      new index.TextField('faviconUrl', ''),
      new index.TextField('appBarLogoUrl', ''),
    ],
    name: 'whiteLabels',
    space: spaces.spaces['mentornity'],
  }),
};

class ValidatorRegex extends InterceptorHandler.InterceptorConfig {
  constructor(regex, message) {
    super('validatorAlphanumericChar');
    this.regexFlags = 'gm';
    this.regex = regex;
    this.message = message;
  }
}
ValidatorRegex.id = 'validatorAlphanumericChar';
class ValidatorRegexHandler extends InterceptorHandler.InterceptorHandler {
  constructor(config) {
    super();
    this.config = config;
  }
  run(context, _, field, value) {
    if (typeof value === 'string' || value instanceof String) {
      if (!new RegExp(this.config.regex, this.config.regexFlags).test(value)) {
        const errorMessage = this.config.message || context.strings.interceptors.validatorRegex.message;
        return {
          error: errorMessage.replace('{title}', field.errorTitle || field.title),
          value,
        };
      }
    }
    return { value };
  }
}
InterceptorHandler.registerInterceptor(ValidatorRegex.id, (config) => new ValidatorRegexHandler(config));

class DvnDataAdapter {
  constructor(labelFieldId, valueFieldId) {
    this.labelFieldId = labelFieldId;
    this.labelFieldIds = labelFieldId.split('+');
    this.valueFieldId = valueFieldId;
  }
  label(item) {
    if (this.labelFieldIds.length > 0) {
      return this.labelFieldIds.map(fieldId => item[fieldId]).join(' ');
    }
    return item[this.labelFieldId];
  }
  value(item) {
    return item[this.valueFieldId];
  }
}

class TableListViewConfig extends viewConfig.DvnAppViewConfig {
  static decode(json) {
    const model = Object.assign(Object.create(TableListViewConfig.prototype), json);
    model.table = tableConfig.TableConfig.decode(model.table);
    return model;
  }
}

exports.DvnDataAdapter = DvnDataAdapter;
exports.RelationReferenceField = RelationReferenceField;
exports.TableListViewConfig = TableListViewConfig;
exports.ValidatorRegex = ValidatorRegex;
exports.dataConfigs = dataConfigs;
