import { get, post, patch, del } from './client.js';

export async function fetchLocations(params = {}) {
  const query = new URLSearchParams(params).toString();
  return get(query ? `/api/location?${query}` : '/api/location');
}

export async function fetchLocation(id) {
  return get(`/api/location/${id}`);
}

export async function createLocation(data) {
  const payload = {
    LocCode: data.Code,
    LocFull: data.Name,
    LocType: data.Type,
    Parent: data.ParentID,
  };
  return post('/api/location', payload);
}

export async function updateLocation(id, data) {
  const payload = {
    LocCode: data.Code,
    LocFull: data.Name,
    LocType: data.Type,
    Parent: data.ParentID,
  };
  return patch(`/api/location/${id}`, payload);
}

export async function deleteLocation(id) {
  return del('/api/location', { body: JSON.stringify({ LocationID: id }) });
}
