import { APIBase } from '@dvn/core/api/apiBase';
import { DataModel } from '@dvn/core/models/data-model';
export class MentornityAnnouncementSelectUsersBridgeApi {
  constructor(organisationId, endpoint, headers, config, onRoleChange, onDataChange, onFetchingChange) {
    this.client = new APIBase(endpoint, headers);
    this.organisationId = organisationId;
    this.config = config;
    this.onDataChange = onDataChange;
    this.onRoleChange = onRoleChange;
    this.onFetchingChange = onFetchingChange;
  }
  addAuthInterceptor(_token) {
    // console.log('MBA.addAuthInterceptor', token);
  }
  // region DataApi
  get(_collection, _id, _query) {
    return undefined;
  }
  async query(_collection) {
    let role = this.getRoleForFetching(_collection) || 'mentors';
    this.onRoleChange(_collection);
    this.onFetchingChange();
    const data = await new Promise((resolve => {
      this.client.get(`organisations/${this.organisationId}/${role}`, { includePassiveMentors: true, expandMetadatas: true }).then(response => {
        var _a, _b;
        role = role === 'all' ? role : role.substring(0, role.length - 1);
        let results = response.results;
        if (role === 'all') {
          results = [...(_a = response.mentors) === null || _a === void 0 ? void 0 : _a.results, ...(_b = response.mentees) === null || _b === void 0 ? void 0 : _b.results];
        }
        resolve({
          results: results.map((user) => {
            var _a;
            let metadata = {};
            if (role !== 'all') {
              (_a = this.config) === null || _a === void 0 ? void 0 : _a.metadatas[role].forEach(field => {
                metadata[field.id] = this.getMetadataValue(user, field);
              });
            }
            return DataModel.decode(Object.assign(Object.assign({ _id: user._id }, (user || {})), metadata));
          }),
          meta: {
            total: results.length,
          }
        });
      }).catch(e => {
        console.log(e);
      });
    }));
    this.onFetchingChange();
    this.onDataChange(data);
    return data;
  }
  getRoleForFetching(role) {
    if (role === 'customMentor') {
      return 'mentors';
    }
    else if (role === 'customMentee') {
      return 'mentees';
    }
    else if (role === 'all') {
      return role;
    }
    else {
      return role + 's';
    }
  }
  getMetadataValue(user, field) {
    var _a;
    const refField = (_a = user === null || user === void 0 ? void 0 : user.metadata) === null || _a === void 0 ? void 0 : _a.data[field.id];
    switch (field.type) {
      case 'ratingStars':
        return Math.floor(refField * 5) || '';
      case 'multipleChoice':
        let string = '';
        (refField || []).forEach((label, i) => {
          string += label + `${(refField === null || refField === void 0 ? void 0 : refField.length) - 1 > i ? ', ' : ''}`;
        });
        return string;
      default:
        return refField;
    }
  }
  update(_collection, _id, _body) {
    return undefined;
  }
}
