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

export default {
  async search(searchModel) {
    const params = JSON.parse(JSON.stringify(searchModel));
    var searchResult = await apiClient
      .post(apiPath.location.search, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async createOrUpdate(locationModel) {
    const params = JSON.parse(JSON.stringify(locationModel));
    var searchResult = await apiClient
      .post(apiPath.location.createOrUpdate, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getById(id) {
    var locationModel = await apiClient
      .get(apiPath.location.getById + id)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return locationModel;
  },
  async delete(id) {
    var result = await apiClient
      .delete(apiPath.location.delete + id)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async loSug(keyword, type, parentId) {
    var result = await apiClient
      .get(
        apiPath.location.loSug +
          "?keyword=" +
          keyword +
          "&type=" +
          type +
          "&parentId=" +
          parentId
      )
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async addressAutocomplete(keyword) {
    var result = await apiClient
      .get(apiPath.location.addressAutocomplete + "?keyword=" + keyword)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async placeDetail(placeId) {
    var result = await apiClient
      .get(apiPath.location.placeDetail + "?placeId=" + placeId)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async getFullAddressByWard(id) {
    var locationModel = await apiClient
      .get(apiPath.location.getFullAddressByWard + id)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return locationModel;
  },
};
