import { n as none, b as fromNullable, p as pipe, m as map, c as isSome } from './index-6869168f.js';
import { a as getOrError } from './index-8f0ba0c8.js';
import { w as withMinimalDuration } from './promise-b835604e.js';
import { B as BackendApiClient, f as fn, u as userApi, U as UserProfileDto } from './api-client-a68152b8.js';
import { S as StoreConfig, a as Store, Q as Query } from './datorama-akita-d2413fd0.js';

class RoleManagementApi {
  async getEditorProfiles() {
    return (await BackendApiClient.instance.url(`users/editors`).get().json());
  }
  async assignRole(profileId, role) {
    const req = {
      command: {
        assignRole: role
      }
    };
    return 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: none,
    editorLoading: false,
    showRemoveEditorDialog: false
  };
}
let RoleManagementStore = class RoleManagementStore extends Store {
  constructor() {
    super(createInitialState());
  }
  setEditorLoading(v) {
    this.update((state) => {
      state.editorLoading = v;
    });
  }
  selectEditor(editor) {
    this.update((state) => {
      state.selectedEditor = fromNullable(editor);
    });
  }
  showRemoveEditorDialog(v) {
    this.update((state) => {
      state.showRemoveEditorDialog = v;
    });
  }
};
RoleManagementStore = __decorate([
  StoreConfig({ name: 'role-management', resettable: true, producerFn: fn })
], RoleManagementStore);
const roleManagementStore = new RoleManagementStore();

class RoleManagementQuery extends 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 userApi.getBasicProfileByEmail(email);
    return pipe(res, map((dto) => dto));
  }
  async getEditorProfiles() {
    const editorProfiles = (await roleManagementApi.getEditorProfiles()).map(UserProfileDto.toDomain);
    roleManagementStore.update((state) => {
      state.editorProfiles = editorProfiles;
    });
  }
  async removeSelectedEditor() {
    roleManagementStore.showRemoveEditorDialog(false);
    roleManagementStore.setEditorLoading(true);
    const editor = 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 withMinimalDuration(this.getBasicProfileByEmail(email), 300);
    if (isSome(user)) {
      await roleManagementApi.assignRole(user.value.profileId, 'accountEditor');
      await this.getEditorProfiles();
    }
    roleManagementStore.setEditorLoading(false);
    return user;
  }
}
const roleManagementService = new RoleManagementService();

export { roleManagementService as a, roleManagementQuery as b, roleManagementStore as r };
