<script>
import { ObjectsGlobal } from "@/components/session/token/SessionUser/Objects/ObjectsGlobal.vue";
import { SessionUser } from "@/components/session/token/SessionUser.vue";
import {ref} from "vue";

/**
 * Local vehicle types cache
 * @type {ref<null>}
 */
export const vehicle_types_cache = ref(null);
/**
 * Get the vehicle type name
 * @param id
 * @param fallback
 * @returns {string}
 */
export const getVehicleTypeName = (id, fallback = null) => {
  if (vehicle_types_cache.value === null) {
    vehicle_types_cache.value = [];
    SessionUser.objects.xlvask_vehicle_types.get.all().then((vehicle_types) => {
      vehicle_types_cache.value = vehicle_types;
    });
  }
  const vehicle_type = vehicle_types_cache.value.find((vehicle_type) => vehicle_type.vehicleTypeId === id);
  return vehicle_type ? vehicle_type.name : fallback === null ? SessionUser.objects.global.language.no_data : fallback;
}

/**
 * The XLVaskVehicleTypes object
 */
export const XLVaskVehicleTypes = {
  meta: {
    title: "Køretøjstyper",
    icon: "fas fa-list",
    description: "Oversigt over køretøjstyper",
    endpoint: "/modules/xlvask/internal/vehicle-types",
    labels: {
      single: "køretøjstype",
      multiple: "køretøjstyper"
    }
  },
  columns: {
    id: {
      label: "ID",
      type: "number",
      sortable: true,
      creation: {
        required: false
      }
    },
    vehicleTypeId: {
      label: "Vehicle Type ID",
      type: "string",
      sortable: true,
      creation: {
        required: true
      }
    },
    product: {
      label: "Produkt",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        return SessionUser.objects.vehicles.columns.type.options();
      },
    },
    name: {
      label: "Navn",
      type: "string",
      sortable: true,
      creation: {
        required: false
      }
    },
  },
  add: async (vehicleTypeId, product, name = null) => {
    return ObjectsGlobal.add.object(
        XLVaskVehicleTypes.meta.endpoint,
        {
          vehicleTypeId: vehicleTypeId,
          product: parseInt(product),
          name: name,
        }
    );
  },
  set: {
    vehicleTypeId: async (id, vehicleTypeId) => {
      return ObjectsGlobal.set.column(
        XLVaskVehicleTypes.meta.endpoint,
        parseInt(id),
        "vehicleTypeId",
        vehicleTypeId
      )
    },
    product: async (id, product) => {
      return ObjectsGlobal.set.column(
        XLVaskVehicleTypes.meta.endpoint,
        parseInt(id),
        "product",
        parseInt(product)
      )
    },
    name: async (id, name) => {
      return ObjectsGlobal.set.column(
        XLVaskVehicleTypes.meta.endpoint,
        parseInt(id),
        "name",
        name
      )
    },
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(XLVaskVehicleTypes.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(XLVaskVehicleTypes.meta.endpoint, id);
    }
  },
  delete: async (id) => {
    return ObjectsGlobal.delete.object(XLVaskVehicleTypes.meta.endpoint, id);
  },
  functions: {
    getVehicleTypeName: getVehicleTypeName,
    /**
     * Show the delete object form
     * @param id
     * @param onAfterSubmit
     * @returns {Promise<SweetAlertResult<Awaited<any>>>}
     */
    showDeleteObjectForm: (id, onAfterSubmit = null) => {
      return ObjectsGlobal.showDeleteObjectForm(XLVaskVehicleTypes, id, onAfterSubmit);
    },
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @param lockedValues
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null, lockedValues = {}) => {
    return ObjectsGlobal.showCreateObjectForm(XLVaskVehicleTypes, onAfterSubmit, lockedValues);
  },
  /**
   * Show the edit object field form
   * @param id
   * @param column
   * @param value
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showEditObjectFieldForm: (id, column, value, onAfterSubmit = null) => {
    return ObjectsGlobal.showEditObjectFieldForm(
        XLVaskVehicleTypes,
        id,
        column,
        value,
        onAfterSubmit
    );
  }
};
</script>