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

/**
 * Local categories cache
 * @type {ref<null>}
 */
export const categories_cache = ref(null);

/**
 * Get the category name
 * @param id
 * @returns {string}
 */
export const getCategoryName = (id) => {
  if (categories_cache.value === null) {
    categories_cache.value = [];
    SessionUser.objects.categories.get.all().then((categories) => {
      categories_cache.value = categories;
    });
  }
  const category = categories_cache.value.find((category) => category.id === parseInt(id));
  return category ? category.name : SessionUser.objects.global.language.no_data;
}

/**
 * The Categories object
 */
export const Categories = {
  meta: {
    title: "Varegrupper",
    icon: "fas fa-list",
    description: "Oversigt over varegrupper",
    endpoint: "/categories",
    labels: {
      single: "varegruppe",
      multiple: "varegrupper"
    }
  },
  columns: {
    id: {
      label: "ID",
      type: "number",
      sortable: true,
      creation: {
        required: false
      }
    },
    name: {
      label: "Navn",
      type: "string",
      sortable: true,
      creation: {
        required: true
      }
    },
    description: {
      label: "Beskrivelse",
      type: "string",
      sortable: true,
      creation: {
        required: false
      }
    },
  },
  add: async (name, description) => {
    return ObjectsGlobal.add.object(
        Categories.meta.endpoint,
        {
          name: name,
          description: description
        }
    );
  },
  set: {
    name: async (id, name) => {
      return ObjectsGlobal.set.column(
        Categories.meta.endpoint,
        id,
        "name",
        name
      )
    },
    description: async (id, description) => {
      return ObjectsGlobal.set.column(
        Categories.meta.endpoint,
        id,
        "description",
        description
      )
    },
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(Categories.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(Categories.meta.endpoint, id);
    }
  },
  functions: {
    getCategoryName: getCategoryName,
    showDeleteObjectForm: (id, onAfterSubmit = null) => {
      return ObjectsGlobal.showDeleteObjectForm(Categories, id, onAfterSubmit);
    },
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null) => {
    return ObjectsGlobal.showCreateObjectForm(Categories, onAfterSubmit);
  },
  /**
   * 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(
        Categories,
        id,
        column,
        value,
        onAfterSubmit
    );
  }
};
</script>