<script>
import {ref} from "vue";
import {authenticatedRequest} from "@/components/session/authenticatedRequest.vue";
import { parseError } from "@/components/request/HandleGlobalError.vue";
import Swal from "sweetalert2";

// Cached vehicles
export const vehicles = ref([]);

export const getUsersVehicles = async () => {
  return await authenticatedRequest("/user/vehicles", "GET")
      .then(
          (response) => {
            vehicles.value = response.data.data;
          }
      )
      .catch(
          (error) => {
            parseError(error, 'addVehicleUser');
          }
      );
}

export const addVehicleUser = async (reg, type, notes) => {
  return await authenticatedRequest("/user/vehicles", "POST", {reg, type, notes})
      .then(
          (response) => {
            vehicles.value.push(response.data.data);
          }
      )
      .catch(
          (error) => {
            parseError(error, 'addVehicleUser');
          }
      );
}

</script>