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

export default {
  async getAgencyCoverage(agencyId) {
    var result = await apiClient
      .get(apiPath.agencyLocation.getAgencyCoverage + "?agencyId=" + agencyId)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return null;
      });
    return result;
  },

  async getAvailableLocationsTree(agencyId, parentId = 0) {
    var result = await apiClient
      .get(
        apiPath.agencyLocation.getAvailableLocationsTree +
          "?agencyId=" +
          agencyId +
          "&parentId=" +
          parentId
      )
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return [];
      });
    return result;
  },

  async addLocation(model) {
    const params = JSON.parse(JSON.stringify(model));
    var result = await apiClient
      .post(apiPath.agencyLocation.addLocation, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {
          Result: 3,
          Message: e.message || "Có lỗi xảy ra",
        };
      });
    return result;
  },

  async removeLocation(model) {
    const params = JSON.parse(JSON.stringify(model));
    var result = await apiClient
      .post(apiPath.agencyLocation.removeLocation, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {
          Result: 3,
          Message: e.message || "Có lỗi xảy ra",
        };
      });
    return result;
  },
};
