'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _reactIntl = require('react-intl');

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _recordDataHelpers = require('../../../helpers/recordDataHelpers');

var _validationHelpers = require('../../../helpers/validationHelpers');

var _errorCodes = require('../../../constants/errorCodes');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.default = pluginContext => {
  var _pluginContext$inputC = pluginContext.inputComponents;
  const CompoundInput = _pluginContext$inputC.CompoundInput,
        OptionPickerInput = _pluginContext$inputC.OptionPickerInput,
        RolesInput = _pluginContext$inputC.RolesInput,
        TextInput = _pluginContext$inputC.TextInput,
        PasswordInput = _pluginContext$inputC.PasswordInput;
  const config = pluginContext.configHelpers.configKey;


  return {
    'ns2:accounts_common': {
      [config]: {
        messages: (0, _reactIntl.defineMessages)({
          errorNotConfirmed: {
            'id': 'field.accounts_common.errorNotConfirmed',
            'defaultMessage': 'Password and confirm password must be identical.'
          }
        }),
        service: {
          ns: 'http://collectionspace.org/services/account'
        },
        validate: ({ data, fieldDescriptor }) => {
          const password = data.get('password');
          const confirmPassword = data.get('confirmPassword');

          if (password !== confirmPassword) {
            return {
              code: _errorCodes.ERR_PW_NOT_CONFIRMED,
              message: fieldDescriptor[config].messages.errorNotConfirmed
            };
          }

          return undefined;
        },
        view: {
          type: CompoundInput
        }
      },
      '@csid': {
        [config]: {
          cloneable: false
        }
      },
      createdAt: {
        [config]: {
          cloneable: false
        }
      },
      updatedAt: {
        [config]: {
          cloneable: false
        }
      },
      metadataProtection: {
        [config]: {
          cloneable: false
        }
      },
      rolesProtection: {
        [config]: {
          cloneable: false
        }
      },
      screenName: {
        [config]: {
          cloneable: false,
          messages: (0, _reactIntl.defineMessages)({
            name: {
              'id': 'field.accounts_common.screenName.name',
              'defaultMessage': 'Full name'
            }
          }),
          required: true,
          view: {
            type: TextInput,
            props: {
              autoComplete: 'off'
            }
          }
        }
      },
      email: {
        [config]: {
          cloneable: false,
          messages: (0, _reactIntl.defineMessages)({
            errorInvalidEmail: {
              'id': 'field.accounts_common.email.errorInvalidEmail',
              'defaultMessage': 'Email is not a valid address. Correct the value {value}.'
            },
            name: {
              'id': 'field.accounts_common.email.name',
              'defaultMessage': 'Email address'
            }
          }),
          required: true,
          validate: ({ data, fieldDescriptor }) => {
            if (!(0, _validationHelpers.isValidEmail)(data)) {
              return {
                code: _errorCodes.ERR_INVALID_EMAIL,
                message: fieldDescriptor[config].messages.errorInvalidEmail,
                value: data
              };
            }

            return undefined;
          },
          view: {
            type: TextInput
          }
        }
      },
      password: {
        [config]: {
          cloneable: false,
          messages: (0, _reactIntl.defineMessages)({
            errorInvalidPassword: {
              'id': 'field.accounts_common.password.errorInvalidPassword',
              'defaultMessage': 'Password must be between 8 and 24 characters.'
            },
            name: {
              'id': 'field.accounts_common.password.name',
              'defaultMessage': 'Password'
            }
          }),
          required: recordData => (0, _recordDataHelpers.isNewRecord)(recordData),
          validate: ({ data, fieldDescriptor }) => {
            if (data && !(0, _validationHelpers.isValidPassword)(data)) {
              return {
                code: _errorCodes.ERR_INVALID_PW,
                message: fieldDescriptor[config].messages.errorInvalidPassword
              };
            }

            return undefined;
          },
          view: {
            type: PasswordInput,
            props: {
              autoComplete: 'new-password'
            }
          }
        }
      },
      confirmPassword: {
        [config]: {
          cloneable: false,
          messages: (0, _reactIntl.defineMessages)({
            name: {
              'id': 'field.accounts_common.passwordConfirmation.name',
              'defaultMessage': 'Confirm password'
            }
          }),
          required: recordData => (0, _recordDataHelpers.isNewRecord)(recordData),
          view: {
            type: PasswordInput,
            props: {
              autoComplete: 'new-password'
            }
          }
        }
      },
      status: {
        [config]: {
          defaultValue: 'active',
          messages: (0, _reactIntl.defineMessages)({
            name: {
              'id': 'field.accounts_common.status.name',
              'defaultMessage': 'Status'
            }
          }),
          required: true,
          view: {
            type: OptionPickerInput,
            props: {
              source: 'accountStatuses'
            }
          }
        }
      },
      userId: {
        [config]: {
          cloneable: false,
          compute: ({ recordData }) => {
            if ((0, _recordDataHelpers.isExistingRecord)(recordData)) {
              // Don't compute a value if this is an existing record.
              return undefined;
            }

            // Set the value to the email address.

            return recordData.getIn(['ns2:accounts_common', 'email']);
          },
          messages: (0, _reactIntl.defineMessages)({
            name: {
              'id': 'field.accounts_common.userId.name',
              'defaultMessage': 'User ID'
            }
          }),
          readOnly: true,
          view: {
            type: TextInput,
            props: {
              readOnly: true
            }
          }
        }
      },
      role: {
        [config]: {
          defaultValue: _immutable2.default.List(),
          messages: (0, _reactIntl.defineMessages)({
            name: {
              'id': 'field.account.role.name',
              'defaultMessage': 'Roles'
            }
          }),
          view: {
            type: RolesInput
          }
        }
      }
    }
  };
};