import { api, ensureCsrf } from "./api.js";

/**
 * @returns {Promise<{ resources: import('axios').AxiosResponse['data'] }>}
 */
export async function fetchDatatableColumnDefaults() {
  await ensureCsrf();
  const { data } = await api.get("/api/superadmin/datatable-column-defaults");
  return data;
}

/**
 * @param {string} resourceKey
 * @param {string[]} visibleKeys
 */
export async function putDatatableColumnDefaults(resourceKey, visibleKeys) {
  await ensureCsrf();
  const enc = encodeURIComponent(resourceKey);
  await api.put(`/api/superadmin/datatable-column-defaults/${enc}`, {
    visibleKeys,
  });
}

/**
 * @param {string} resourceKey
 */
export async function deleteDatatableColumnDefaults(resourceKey) {
  await ensureCsrf();
  const enc = encodeURIComponent(resourceKey);
  await api.delete(`/api/superadmin/datatable-column-defaults/${enc}`);
}

/**
 * @returns {Promise<{ data: { id: number, name: string, email: string }[] }>}
 */
export async function fetchSuperadminUsersMinimal() {
  await ensureCsrf();
  const { data } = await api.get("/api/superadmin/users-minimal");
  return data;
}

/**
 * @param {number} userId
 * @param {string} tableKey
 */
export async function postUserTableStateReset(userId, tableKey) {
  await ensureCsrf();
  await api.post("/api/superadmin/user-table-state/reset", {
    userId,
    tableKey,
  });
}
