import { T as TableConfig, S as Space, d as decodeFields } from './table-config-3820fb34.js';
import { R as ReferenceField } from './reference-field-a5b414d1.js';

class MeetingStructureConfig extends TableConfig {
  static decode(json) {
    const model = Object.assign(Object.create(MeetingStructureConfig.prototype), json);
    model.space = Space.decode(model.space);
    return model;
  }
}

var RuleType;
(function (RuleType) {
  RuleType["comparison"] = "comparison";
  RuleType["conditionalComparison"] = "conditionalComparison";
})(RuleType || (RuleType = {}));
var FieldType;
(function (FieldType) {
  FieldType["number"] = "number";
  FieldType["range"] = "range";
  FieldType["singleChoice"] = "single-choice";
  FieldType["multipleChoice"] = "multipleChoice";
  FieldType["text"] = "text";
})(FieldType || (FieldType = {}));
var Comparison;
(function (Comparison) {
  Comparison["eq"] = "eq";
  Comparison["ne"] = "ne";
  Comparison["lt"] = "lt";
  Comparison["lte"] = "lte";
  Comparison["gt"] = "gt";
  Comparison["gte"] = "gte";
  Comparison["in"] = "in";
  Comparison["contain"] = "contain";
  Comparison["ncontain"] = "ncontain";
  Comparison["similar"] = "similar";
  Comparison["different"] = "different";
})(Comparison || (Comparison = {}));
class Rule {
  constructor(comparisons, type) {
    this.comparisons = comparisons;
    this.type = type;
  }
  static decode(json) {
    return Object.assign(Object.create(Rule.prototype), json);
  }
  isValid() {
    if (this.type === RuleType.comparison) {
      // if this is a comparison rule, it should contain one
      // comparison and that should contain firstField, secondField and comparison
      return this.comparisons.length === 1
        && !!this.comparisons[0].firstField
        && !!this.comparisons[0].secondField
        && !!this.comparisons[0].comparison;
    }
    return true;
  }
}

const getPairConfigMemberCollection = (pairConfig, fieldId) => {
  return pairConfig.fields.find(field => ((field.type === ReferenceField.type) && field.id === fieldId)).otherCollection;
};
const getPairConfigSelectionConfigForRole = (pairConfig, roleName) => {
  return pairConfig.selection ? pairConfig.selection[roleName] : undefined;
};
const getPairConfigSelectionConfigTargetingRole = (pairConfig, roleName) => {
  let config;
  Object.keys((pairConfig.selection || {})).forEach(roleNameThatSelects => {
    const selectionConfig = SelectionConfig.decode(pairConfig.selection[roleNameThatSelects]);
    if (selectionConfig.roleNameToSelect === roleName && selectionConfig.status === 'active') {
      config = pairConfig.selection[roleNameThatSelects];
    }
  });
  return config;
};
class PairConfig extends TableConfig {
  static decode(json) {
    const model = Object.assign(Object.create(PairConfig.prototype), json);
    model.fields = decodeFields(model.fields);
    model.space = Space.decode(model.space);
    // selection
    Object.keys(model.selection || {}).forEach(memberRoleName => {
      model.selection[memberRoleName] = SelectionConfig.decode(model.selection[memberRoleName]);
    });
    if (model.structure) {
      model.structure = MeetingStructureConfig.decode(model.structure);
    }
    model.actionPermissions = model.actionPermissions || {};
    return model;
  }
}
class SelectionConfig {
  static decode(json) {
    var _a;
    const model = Object.assign(Object.create(SelectionConfig.prototype), json);
    model.rules = (_a = model.rules) === null || _a === void 0 ? void 0 : _a.map(rule => Rule.decode(rule));
    return model;
  }
  isActive() {
    return this.status === 'active';
  }
}

export { Comparison as C, MeetingStructureConfig as M, PairConfig as P, Rule as R, RuleType as a, getPairConfigSelectionConfigForRole as b, getPairConfigSelectionConfigTargetingRole as c, getPairConfigMemberCollection as g };
