'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 }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

exports.default = function (pluginContext) {
  var _ns3Accounts_common;

  var _pluginContext$inputC = pluginContext.inputComponents,
      CompoundInput = _pluginContext$inputC.CompoundInput,
      OptionPickerInput = _pluginContext$inputC.OptionPickerInput,
      RolesInput = _pluginContext$inputC.RolesInput,
      TextInput = _pluginContext$inputC.TextInput,
      PasswordInput = _pluginContext$inputC.PasswordInput;
  var config = pluginContext.configHelpers.configKey;


  return {
    'ns3:accounts_common': (_ns3Accounts_common = {}, _defineProperty(_ns3Accounts_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: function validate(data, path, recordData, fieldDescriptor) {
        var password = data.get('password');
        var confirmPassword = data.get('confirmPassword');

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

        return undefined;
      },
      view: {
        type: CompoundInput
      }
    }), _defineProperty(_ns3Accounts_common, '@csid', _defineProperty({}, config, {
      cloneable: false
    })), _defineProperty(_ns3Accounts_common, 'createdAt', _defineProperty({}, config, {
      cloneable: false
    })), _defineProperty(_ns3Accounts_common, 'updatedAt', _defineProperty({}, config, {
      cloneable: false
    })), _defineProperty(_ns3Accounts_common, 'metadataProtection', _defineProperty({}, config, {
      cloneable: false
    })), _defineProperty(_ns3Accounts_common, 'rolesProtection', _defineProperty({}, config, {
      cloneable: false
    })), _defineProperty(_ns3Accounts_common, 'screenName', _defineProperty({}, 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'
        }
      }
    })), _defineProperty(_ns3Accounts_common, 'email', _defineProperty({}, 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: function validate(data, path, recordData, fieldDescriptor) {
        if (!(0, _validationHelpers.isValidEmail)(data)) {
          return {
            code: _errorCodes.ERR_INVALID_EMAIL,
            message: fieldDescriptor[config].messages.errorInvalidEmail,
            value: data
          };
        }

        return undefined;
      },
      view: {
        type: TextInput
      }
    })), _defineProperty(_ns3Accounts_common, 'password', _defineProperty({}, 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: function required(recordData) {
        return (0, _recordDataHelpers.isNewRecord)(recordData);
      },
      validate: function validate(data, path, recordData, 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'
        }
      }
    })), _defineProperty(_ns3Accounts_common, 'confirmPassword', _defineProperty({}, config, {
      cloneable: false,
      messages: (0, _reactIntl.defineMessages)({
        name: {
          'id': 'field.accounts_common.passwordConfirmation.name',
          'defaultMessage': 'Confirm password'
        }
      }),
      required: function required(recordData) {
        return (0, _recordDataHelpers.isNewRecord)(recordData);
      },
      view: {
        type: PasswordInput,
        props: {
          autoComplete: 'new-password'
        }
      }
    })), _defineProperty(_ns3Accounts_common, 'status', _defineProperty({}, config, {
      defaultValue: 'active',
      messages: (0, _reactIntl.defineMessages)({
        name: {
          'id': 'field.accounts_common.status.name',
          'defaultMessage': 'Status'
        }
      }),
      required: true,
      view: {
        type: OptionPickerInput,
        props: {
          source: 'accountStatuses'
        }
      }
    })), _defineProperty(_ns3Accounts_common, 'userId', _defineProperty({}, config, {
      cloneable: false,
      compute: function compute(value, path, data) {
        if ((0, _recordDataHelpers.isExistingRecord)(data)) {
          // Don't compute a value if this is an existing record.
          return undefined;
        }

        // Set the value to the email address.

        return data.getIn(['ns3: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
        }
      }
    })), _defineProperty(_ns3Accounts_common, 'role', _defineProperty({}, config, {
      defaultValue: _immutable2.default.List(),
      messages: (0, _reactIntl.defineMessages)({
        name: {
          'id': 'field.account.role.name',
          'defaultMessage': 'Roles'
        }
      }),
      view: {
        type: RolesInput
      }
    })), _ns3Accounts_common)
  };
};