import apiClient from "@/infrastructure/apiClient";
import apiPath from "@/infrastructure/apiPath";

export default {
  async getList(searchModel) {
    const params = JSON.parse(JSON.stringify(searchModel));
    var searchResult = await apiClient
      .post(apiPath.permission.search, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getPermissions() {
    var result = await apiClient
      .get(apiPath.permission.getPermissions)
      .then((r) => r.data)
      .catch((e) => {
        console.log(e);
        return [];
      });
    return result;
  },

  async setPermission(setPermissionModel) {
    const params = JSON.parse(JSON.stringify(setPermissionModel));
    var result = await apiClient
      .post(apiPath.permission.setPermission, params)
      .then((r) => r.data)
      .catch((e) => {
        console.log(e);
        return [];
      });
    return result;
  },
  async create(data) {
    const params = JSON.parse(JSON.stringify(data));
    var result = await apiClient
      .post(apiPath.permission.createOrUpdate, params)
      .then((r) => r.data)
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async update(data) {
    const params = JSON.parse(JSON.stringify(data));
    var result = await apiClient
      .post(apiPath.permission.createOrUpdate, params)
      .then((r) => r.data)
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async delete(id) {
    var result = await apiClient
      .delete(apiPath.permission.delete.replace(":id", id))
      .then((r) => r.data)
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async getRoles(permissionId) {
    var result = await apiClient
      .get(apiPath.permission.getRoles.replace(":id", permissionId))
      .then((r) => r.data)
      .catch((e) => {
        console.log(e);
        return [];
      });
    return result;
  },
  async updateRoles(updateRolesModel) {
    const params = JSON.parse(JSON.stringify(updateRolesModel));
    var result = await apiClient
      .put(apiPath.permission.updateRoles, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
};
