'use strict';

const dataModel = require('./data-model-9afb7f54.js');
const tableConfig = require('./table-config-8b14c7d3.js');
const string = require('./string-de672b69.js');
const User = require('./User-e889279b.js');

class MembershipConfig extends tableConfig.TableConfig {
  static decode(json) {
    const model = Object.assign(Object.create(MembershipConfig.prototype), json);
    model.fields = tableConfig.decodeFields(model.fields);
    model.space = tableConfig.Space.decode(model.space);
    return model;
  }
  roleNameCapital() {
    return string.capitalize(this.roleName);
  }
  ;
}

const isMembershipActive = (membership) => membership.status === 'active';
const doesMembershipNeedAgreementConsent = (membership) => {
  const agreements = membership.config.agreements || [];
  const consents = membership.consents || [];
  return agreements.filter(agreement => !consents.some(consent => consent.agreement._id === agreement._id)).length > 0;
};
const doesMembershipNeedDataCompletion = (membership) => {
  if (!membership.config.enforceRequiredInfo) {
    return false;
  }
  const fields = membership.config.fields || [];
  for (let i = 0; i < fields.length; i++) {
    const field = fields[i];
    if (field.required && !membership[field.id]) {
      return true;
    }
  }
  return false;
};
class Membership extends dataModel.DataModel {
  static decode(json) {
    const model = Object.assign(Object.create(Membership.prototype), json);
    model.config = MembershipConfig.decode(model.config);
    model.user = model.user ? User.User.decode(model.user) : model.user;
    return model;
  }
  requireDataCompletion() {
    if (!this.config.enforceRequiredInfo) {
      return false;
    }
    const fields = this.config.fields || [];
    for (let i = 0; i < fields.length; i++) {
      const field = fields[i];
      if (field.required && !this[field.id]) {
        return true;
      }
    }
    return false;
  }
}

exports.Membership = Membership;
exports.MembershipConfig = MembershipConfig;
exports.doesMembershipNeedAgreementConsent = doesMembershipNeedAgreementConsent;
exports.doesMembershipNeedDataCompletion = doesMembershipNeedDataCompletion;
exports.isMembershipActive = isMembershipActive;
