'use strict';

const dataModel = require('./data-model-f9df493f.js');
const membershipConfig = require('./membership-config-47f4733e.js');
const User = require('./User-ed078a17.js');

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.MembershipConfig.decode(model.config);
    model.user = User.User.decode(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.doesMembershipNeedAgreementConsent = doesMembershipNeedAgreementConsent;
exports.doesMembershipNeedDataCompletion = doesMembershipNeedDataCompletion;
exports.isMembershipActive = isMembershipActive;
