import { APIBase } from '@dvn/core/api/apiBase';
import { Collection } from './models';
function generateQueryParams(query = {}, skip = 0, sort = '', limit = 25) {
  const params = Object.assign(Object.assign({}, query), { skip,
    limit });
  if (sort && sort != '') {
    params.sort = sort;
  }
  return params;
}
const extractList = promise => (new Promise((resolve, reject) => {
  promise.then(response => resolve(response.results), error => reject(error));
}));
export class MentornityApi {
  constructor(endpoint, defaultHeaders = {}) {
    // region common
    this.create = (collection, body) => this.api.post(collection, body);
    this.find = (collection, params) => this.api.get(`${collection}`, params);
    this.get = (collection, id, params) => this.api.get(`${collection}/${id}`, params);
    this.update = (collection, id, body) => this.api.put(`${collection}/${id}`, body);
    this.delete = (collection, id) => this.api.delete(`${collection}/${id}`);
    // endregion
    // region Auth
    this.login = (email, password, client) => (this.api.post('login', { email, password, client }));
    this.saveConsentForPolicy = (userId, policyId, page) => this.api.post(`users/${userId}/consents`, { policyId, page });
    this.getPrivacyPolicy = language => this.api.get(`policies/privacy-policy`, { language });
    // endregion
    // region session
    /**
     * Used to gets session data. Returns only white label info if there is no account.
     *
     * @param domain of the current web site. (used to determine the white label)
     */
    this.getSessionData = (domain) => this.api.get(`sessionData?domain=${domain}`);
    // endregion
    // region organisation
    this.getOrganisation = (organisationId) => this.api.get(`organisations/${organisationId}`);
    this.getOrganisationWithDomain = domain => this.api.get(`organisations/domain/${domain}`);
    this.getOrganisationConfig = (organisationId) => this.api.get(`organisations/${organisationId}/config`);
    this.getOrganisationMeetingGoalConfig = (organisationId) => this.api.get(`organisations/${organisationId}/meetingGoalConfig`);
    this.getViewConfigs = (organisationId) => this.api.get(`organisations/${organisationId}/view-configs`);
    this.getViewConfig = (organisationId, viewName) => this.api.get(`organisations/${organisationId}/view-configs/${viewName}`);
    this.createViewConfig = (viewConfigWrapper) => this.api.post('view-configs', viewConfigWrapper);
    this.updateViewConfig = (id, viewConfigWrapper) => this.api.put(`view-configs/${id}`, viewConfigWrapper);
    // endregion
    this.createInvitation = (data) => this.api.post('invitations', data);
    this.getInvitation = (id) => this.api.get(`invitations/${id}`, { expand: 'organisation,invitee' });
    this.acceptInvitation = (invitationId, password) => this.api.post(`invitations/${invitationId}/accept`, { password });
    // region members
    this.getMentees = (organisationId, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${organisationId}/mentees`, generateQueryParams(query, skip, sort, limit));
    this.getMentors = (organisationId, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${organisationId}/mentors`, generateQueryParams(query, skip, sort, limit));
    this.getGroups = (id, skip = 0, limit = 50, title = '', expandCreatedBy = false) => (this.api.get(`organisations/${id}/groups`, { skip, limit, title, expandCreatedBy }));
    this.getGroup = (id) => (this.api.get(`groups/${id}`));
    this.getMentorshipOfUser = (organisationId, userId) => this.api.get(`organisations/${organisationId}/mentors/${userId}/mentorship`);
    this.getMembershipApplicationForm = (organisationId, role) => this.api.get(`organisations/${organisationId}/${role}/applications/form`);
    this.createMembershipApplication = (organisationId, role, data) => this.api.post(`organisations/${organisationId}/${role}/applications`, data);
    // endregion
    // region metadatas
    this.getMetadataOfUser = (organisationId, role, userId) => this.api.get(`organisations/${organisationId}/${role}/${userId}/metadata`);
    // endregion
    // region subscriptions
    this.getOrganisations = (query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations`, generateQueryParams(query, skip, sort, limit));
    this.createSubscription = (subscription) => this.api.post(`subscriptions`, subscription);
    this.followUnfollowSubscription = (subscriptionId, action) => this.api.post(`subscriptions/${subscriptionId}/${action}`, {});
    // endregion
    // region subscriptions
    this.getSubscriptionsOfMentors = (id, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${id}/mentors`, generateQueryParams(Object.assign(Object.assign({}, query), { appendSubscriptions: true }), skip, sort, limit));
    this.completeSubscription = (subscriptionId) => this.api.put(`${Collection.Subscriptions}/${subscriptionId}`, { status: 'completed' });
    this.deleteSubscription = (subscriptionId) => this.api.delete(`${Collection.Subscriptions}/${subscriptionId}`);
    // endregion
    this.getMeetings = (organisationId, memberRolePath, userId, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${organisationId}/${memberRolePath}/${userId}/meetings`, generateQueryParams(query, skip, sort, limit));
    this.getUsersMeetingsChronologically = (accountId, organisationId, skip) => extractList(this.api.get(`users/${accountId}/events`, { organisationId, skip, limit: 50 * 2, appendFeedbacks: true }));
    this.getGroupsMeetingsChronologically = (groupId, skip) => extractList(this.api.get(`groups/${groupId}/events`, { skip, limit: 50, appendFeedbacks: true }));
    this.getMeetingsInMeetingGoal = (id, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${id}/meetingsInMeetingGoal`, generateQueryParams(query, skip, sort, limit));
    this.getUsersWithMeetingsInMeetingGoal = (id, userType, query, skip = 0, sort, limit) => this.api.get(`organisations/${id}/${userType}`, generateQueryParams(Object.assign(Object.assign({}, query), { expandMetadatas: true, includePassiveMentors: true, appendMeetingsInMeetingGoal: true, expand: 'events(guests)' }), skip, sort, limit));
    this.getCalendarBundleList = (domain, startDate, endDate) => this.api.get(`organisations/${domain}/calendarBundleList`, { startDate, endDate });
    this.getAnnouncements = (id, role, skip = 0, limit = 50) => extractList(this.api.get(`organisations/${id}/announcements`, { skip, limit, role }));
    this.createForm = (data) => this.api.post('forms', data);
    // region forms
    this.createFormResponse = (data) => this.api.post('formResponses', data);
    // region white labels
    this.createWhiteLabel = (data) => this.api.post('whiteLabels', data);
    this.updateWhiteLabel = (id, data) => this.api.put(`whiteLabels/${id}`, data);
    this.deleteWhiteLabel = (id) => this.api.delete(`whiteLabels/${id}`);
    // endregion
    this.addAuthInterceptor = (token, sessionToken) => {
      this.api.addAuthInterceptor(token, sessionToken);
    };
    this.api = new APIBase(endpoint, defaultHeaders);
  }
  cloneOrganisation(body) {
    return this.api.post('organisations/clone', body);
  }
  importUser(id, userType, body) {
    return this.api.post(`organisations/${id}/${userType}/import`, body);
  }
  removeOrganisationAdmin(userId, body) {
    return this.api.post(`users/${userId}/recallRole`, body);
  }
  eraseUser(userId) {
    return this.api.post(`users/${userId}/erase`, {});
  }
  changeOrganisationDomain({ domain, organisationId }) {
    return this.api.put(`organisations/${organisationId}/domain`, { domain });
  }
}
