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

export default {
  async createOrUpdate(createOrUpdateModel) {
    const params = JSON.parse(JSON.stringify(createOrUpdateModel));

    var roleResult = await apiClient
      .post(apiPath.role.createOrUpdate, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async search(searchModel) {
    const params = JSON.parse(JSON.stringify(searchModel));

    var searchResult = await apiClient
      .post(apiPath.role.search, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async delete(roleId) {
    var deleteResult = await apiClient
      .delete(apiPath.role.delete + roleId)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return deleteResult;
  },
  async getById(roleId) {
    var roleResult = await apiClient
      .get(apiPath.role.getById + roleId)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async getRoles() {
    var roleResult = await apiClient
      .get(apiPath.role.getRoles)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async setRole(setRoleModel) {
    const params = JSON.parse(JSON.stringify(setRoleModel));

    var roleResult = await apiClient
      .put(apiPath.role.setRole, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
  async getList() {
    var roleResult = await apiClient
      .get(apiPath.permission.availableRoles)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
};
