"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactIntl = require("react-intl");
var _immutable = _interopRequireDefault(require("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 }; }
const isMetadataImmutable = _ref => {
  let {
    recordData
  } = _ref;
  return recordData.getIn(['ns2:accounts_common', 'metadataProtection']) === 'immutable';
};
const areRolesImmutable = _ref2 => {
  let {
    recordData
  } = _ref2;
  return recordData.getIn(['ns2:accounts_common', 'rolesProtection']) === 'immutable';
};
var _default = configContext => {
  const {
    CompoundInput,
    OptionPickerInput,
    RolesInput,
    TextInput,
    PasswordInput
  } = configContext.inputComponents;
  const {
    configKey: config
  } = configContext.configHelpers;
  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: _ref3 => {
          let {
            data,
            fieldDescriptor
          } = _ref3;
          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: {
              // Suppress Chrome autofill
              autoComplete: 'cspace-name',
              readOnly: isMetadataImmutable
            }
          }
        }
      },
      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: _ref4 => {
            let {
              data,
              fieldDescriptor
            } = _ref4;
            if (!(0, _validationHelpers.isValidEmail)(data)) {
              return {
                code: _errorCodes.ERR_INVALID_EMAIL,
                message: fieldDescriptor[config].messages.errorInvalidEmail,
                value: data
              };
            }
            return undefined;
          },
          view: {
            type: TextInput,
            props: {
              // Suppress Chrome autofill
              autoComplete: 'cspace-email',
              readOnly: isMetadataImmutable
            }
          }
        }
      },
      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: _ref5 => {
            let {
              recordData
            } = _ref5;
            return (0, _recordDataHelpers.isNewRecord)(recordData);
          },
          validate: _ref6 => {
            let {
              data,
              fieldDescriptor
            } = _ref6;
            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',
              readOnly: isMetadataImmutable
            }
          }
        }
      },
      confirmPassword: {
        [config]: {
          cloneable: false,
          messages: (0, _reactIntl.defineMessages)({
            name: {
              "id": "field.accounts_common.passwordConfirmation.name",
              "defaultMessage": "Confirm password"
            }
          }),
          required: _ref7 => {
            let {
              recordData
            } = _ref7;
            return (0, _recordDataHelpers.isNewRecord)(recordData);
          },
          view: {
            type: PasswordInput,
            props: {
              autoComplete: 'new-password',
              readOnly: isMetadataImmutable
            }
          }
        }
      },
      status: {
        [config]: {
          defaultValue: 'active',
          messages: (0, _reactIntl.defineMessages)({
            name: {
              "id": "field.accounts_common.status.name",
              "defaultMessage": "Status"
            }
          }),
          required: true,
          view: {
            type: OptionPickerInput,
            props: {
              readOnly: isMetadataImmutable,
              source: 'accountStatuses'
            }
          }
        }
      },
      userId: {
        [config]: {
          cloneable: false,
          compute: _ref8 => {
            let {
              recordData
            } = _ref8;
            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"
            }
          }),
          view: {
            type: TextInput,
            props: {
              readOnly: true
            }
          }
        }
      },
      roleList: {
        [config]: {
          view: {
            type: CompoundInput
          }
        },
        role: {
          [config]: {
            defaultValue: _immutable.default.List(),
            messages: (0, _reactIntl.defineMessages)({
              name: {
                "id": "field.account.role.name",
                "defaultMessage": "Roles"
              }
            }),
            view: {
              type: RolesInput,
              props: {
                readOnly: areRolesImmutable
              }
            }
          }
        }
      }
    }
  };
};
exports.default = _default;