<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 product options cache
 * @type {ref<null>}
 */
export const product_options_cache = ref(null);

/**
 * Get the product options
 * @param id {number}
 * @returns {string}
 */
export const getProductOptions = async (id) => {
  if (product_options_cache.value === null) {
    product_options_cache.value = [];
    await SessionUser.objects.product_options.get.all().then((product_options) => {
      product_options_cache.value = product_options;
    });
  }
  const product_option = product_options_cache.value.find((product_option) => product_option.id === parseInt(id));
  return product_option ? product_option.name : SessionUser.objects.global.language.no_data;
}



    /**
     * The Product Options object
     */
    export const ProductOptions = {
      meta: {
        title: "Produkt tilvalg",
        icon: "fas fa-list",
        description: "Oversigt over produkt tilvalg",
        endpoint: "/product/options",
        labels: {
          single: "produkt tilvalg",
          multiple: "produkt tilvalg"
        }
      },
      columns: {
        id: {
          label: "ID",
          type: "number",
          sortable: true,
          creation: {
            required: false
          }
        },
        product_id: {
          label: "Produkt ID",
          type: "select",
          sortable: true,
          creation: {
            required: true
          },
          options: async () => {
            console.log("Getting products");
            return SessionUser.objects.products.get.all();
          }
        },
        option_id: {
          label: "Tilvalgs produkt ID",
          type: "select",
          sortable: true,
          creation: {
            required: true
          },
          options: async () => {
            console.log("Getting product options");
            return SessionUser.objects.products.get.all();
          }
        },
        name: {
          label: "Kaldenavn for tilvalg",
          type: "string",
          sortable: true,
          creation: {
            required: false
          }
        },
        min: {
          label: "Minimum antal",
          type: "number",
          sortable: true,
          creation: {
            required: false
          }
        },
        max: {
          label: "Maksimum antal",
          type: "number",
          sortable: true,
          creation: {
            required: false
          }
        },
      },
      add: async (product_id, option_id) => {
        return ObjectsGlobal.add.object(
            ProductOptions.meta.endpoint,
            {
              product_id: parseInt(product_id),
              option_id: parseInt(option_id)
            }
        );
      },
      set: {
        name: async (id, name) => {
          return ObjectsGlobal.set.column(
              ProductOptions.meta.endpoint,
              id,
              "name",
              name
          )
        },
        min: async (id, min) => {
          return ObjectsGlobal.set.column(
              ProductOptions.meta.endpoint,
              id,
              "min",
              parseInt(min)
          )
        },
        max: async (id, max) => {
          return ObjectsGlobal.set.column(
              ProductOptions.meta.endpoint,
              id,
              "max",
              parseInt(max)
          )
        },
        product_id: async (id, product_id) => {
          return ObjectsGlobal.set.column(
              ProductOptions.meta.endpoint,
              id,
              "product_id",
              product_id
          )
        },
        option_id: async (id, option_id) => {
          return ObjectsGlobal.set.column(
              ProductOptions.meta.endpoint,
              id,
              "option_id",
              option_id
          )
        },
      },
      get: {
        all: async () => {
          return ObjectsGlobal.get.objects(ProductOptions.meta.endpoint);
        },
        single: async (id) => {
          return ObjectsGlobal.get.object(ProductOptions.meta.endpoint, id);
        }
      },
      delete: async (id) => {
        return ObjectsGlobal.delete.object(ProductOptions.meta.endpoint, id);
      },
      functions: {
        getProductOptions: getProductOptions,
        getProductOptionName: (id) => {
          return getProductOptions(id);
        }
      },
      /**
       * Show the create object form
       * @param onAfterSubmit {function}
       * @param lockedValues {object}
       * @returns {Promise<SweetAlertResult<Awaited<any>>>}
       */
      showCreateObjectForm: (onAfterSubmit = null, lockedValues = {}) => {
        return ObjectsGlobal.showCreateObjectForm(ProductOptions, 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(
            ProductOptions,
            id,
            column,
            value,
            onAfterSubmit
        );
      }
    };
</script>