<script>
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
import { ref } from "vue";

// Define the departments
export const departments = ref([]);
export const isLoading = ref(true);

// Get the departments from the API
export const getDepartments = async () => {
  isLoading.value = true;
  const request = await authenticatedRequest('/departments','get');
  departments.value = request.data.data;
  isLoading.value = false;
  console.log(departments.value);
};

// Check if a department exists by ID
export const departmentExists = (id) => {
  return departments.value.find(department => department.id === id) !== undefined;
};

// Get the department name by ID
export const getDepartmentName = (id) => {

  // Check if the departments have been loaded
  if (departments.value.length === 0) {
    // Return the id if the departments have not been loaded (This will be replaced by the department name when the departments are loaded)
    return id;
  }
  // Check if the department exists in the departments array
  if (departments.value.find(department => department.id === id) === undefined) {
    return '';
  }
  // Return the department name
  return departments.value.find(department => department.id === id).name;
};

// Get the department description by ID
export const getDepartmentDescription = (id) => {
  return departments.value.find(department => department.id === id).description;
};
</script>