import { decodeFields } from '@dvn/core/doldurma/src/decoder';
import { ReferenceField } from '@dvn/core/doldurma/src/fields/reference';
import { MeetingStructureConfig } from '@dvn/core/models/meeting-structure-config';
import { Rule } from '@dvn/core/models/rule';
import { Space } from '@dvn/core/models/space';
import { TableConfig } from '@dvn/core/models/table-config';
export const getPairConfigMemberCollection = (pairConfig, fieldId) => {
  return pairConfig.fields.find(field => ((field.type === ReferenceField.type) && field.id === fieldId)).otherCollection;
};
export const getPairConfigSelectionConfigForRole = (pairConfig, roleName) => {
  return pairConfig.selection ? pairConfig.selection[roleName] : undefined;
};
export 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;
};
export 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;
  }
}
export 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';
  }
}
