import { DataModel } from '@dvn/core/models/data-model';
import { MembershipConfig } from '@dvn/core/models/membership-config';
import { User } from '@dvn/kimlik/api/models';
export const isMembershipActive = (membership) => membership.status === 'active';
export 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;
};
export 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;
};
export class Membership extends DataModel {
  static decode(json) {
    const model = Object.assign(Object.create(Membership.prototype), json);
    model.config = MembershipConfig.decode(model.config);
    model.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;
  }
}
