'use strict';

const index = require('./index-4f00f593.js');
const index$1 = require('./index-889ce6c6.js');
const promise = require('./promise-f77cab90.js');
const apiClient = require('./api-client-00edc9b6.js');
const datoramaAkita = require('./datorama-akita-bb801820.js');

class RoleManagementApi {
  async getEditorProfiles() {
    return (await apiClient.BackendApiClient.instance.url(`users/editors`).get().json());
  }
  async assignRole(profileId, role) {
    const req = {
      command: {
        assignRole: role
      }
    };
    return apiClient.BackendApiClient.instance
      .url(`users/${profileId}`)
      .post(req)
      .badRequest(() => {
      throw new Error('Failed to execute user command');
    })
      .res();
  }
}
const roleManagementApi = new RoleManagementApi();

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
function createInitialState() {
  return {
    editorProfiles: [],
    selectedEditor: index.none,
    editorLoading: false,
    showRemoveEditorDialog: false
  };
}
let RoleManagementStore = class RoleManagementStore extends datoramaAkita.Store {
  constructor() {
    super(createInitialState());
  }
  setEditorLoading(v) {
    this.update((state) => {
      state.editorLoading = v;
    });
  }
  selectEditor(editor) {
    this.update((state) => {
      state.selectedEditor = index.fromNullable(editor);
    });
  }
  showRemoveEditorDialog(v) {
    this.update((state) => {
      state.showRemoveEditorDialog = v;
    });
  }
};
RoleManagementStore = __decorate([
  datoramaAkita.StoreConfig({ name: 'role-management', resettable: true, producerFn: apiClient.fn })
], RoleManagementStore);
const roleManagementStore = new RoleManagementStore();

class RoleManagementQuery extends datoramaAkita.Query {
  constructor(store) {
    super(store);
    this.store = store;
    this.editorProfiles$ = this.select((state) => state.editorProfiles);
    this.editorLoading$ = this.select((state) => state.editorLoading);
    this.showRemoveEditorDialog$ = this.select((state) => state.showRemoveEditorDialog);
    this.selectedEditor$ = this.select((state) => state.selectedEditor);
  }
}
const roleManagementQuery = new RoleManagementQuery(roleManagementStore);

class RoleManagementService {
  async getBasicProfileByEmail(email) {
    const res = await apiClient.userApi.getBasicProfileByEmail(email);
    return index.pipe(res, index.map((dto) => dto));
  }
  async getEditorProfiles() {
    const editorProfiles = (await roleManagementApi.getEditorProfiles()).map(apiClient.UserProfileDto.toDomain);
    roleManagementStore.update((state) => {
      state.editorProfiles = editorProfiles;
    });
  }
  async removeSelectedEditor() {
    roleManagementStore.showRemoveEditorDialog(false);
    roleManagementStore.setEditorLoading(true);
    const editor = index$1.getOrError(roleManagementQuery.getValue().selectedEditor);
    await roleManagementApi.assignRole(editor.basicProfile.profileId, 'user');
    await this.getEditorProfiles();
    roleManagementStore.setEditorLoading(false);
  }
  showRemoveEditorDialog(editorProfile) {
    roleManagementStore.selectEditor(editorProfile);
    roleManagementStore.showRemoveEditorDialog(true);
  }
  async addEditorByEmail(email) {
    roleManagementStore.setEditorLoading(true);
    const user = await promise.withMinimalDuration(this.getBasicProfileByEmail(email), 300);
    if (index.isSome(user)) {
      await roleManagementApi.assignRole(user.value.profileId, 'accountEditor');
      await this.getEditorProfiles();
    }
    roleManagementStore.setEditorLoading(false);
    return user;
  }
}
const roleManagementService = new RoleManagementService();

exports.roleManagementQuery = roleManagementQuery;
exports.roleManagementService = roleManagementService;
exports.roleManagementStore = roleManagementStore;
