<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";



    /**
     * The Notifications object
     */
    export const Notifications = {
      meta: {
        title: "Notifikationer",
        icon: "fas fa-bell",
        description: "Notifikationer",
        endpoint: "/notifications",
        labels: {
          single: "notifikation",
          multiple: "notifikationer"
        },
      },
      columns: {
        id: {
          label: "ID",
          type: "number",
          sortable: true,
          creation: {
            required: false
          }
        },
        type: {
          label: "Type",
          type: "string",
          sortable: true,
          creation: {
            required: true
          },
        },
        user_id: {
          label: "Bruger ID",
          type: "number",
          sortable: true,
          creation: {
            required: true
          },
        },
        data: {
          label: "Data",
          type: "Array",
          sortable: true,
          creation: {
            required: false
          },
        },
        created_at: {
          label: "Oprettet",
          type: "date",
          sortable: true,
          creation: {
            required: false
          }
        },
      },
      add: async (type, user_id, data = null) => {
        return ObjectsGlobal.add.object(
            Notifications.meta.endpoint,
            {
              user_id: parseInt(user_id),
              type: type,
              data: data
            }
        );
      },
      set: {
        type: async (id, type) => {
          return ObjectsGlobal.set.column(
              Notifications.meta.endpoint,
              id,
              "type",
              type
          )
        },
        user_id: async (id, user_id) => {
          return ObjectsGlobal.set.column(
              Notifications.meta.endpoint,
              id,
              "user_id",
              parseInt(user_id)
          )
        },
        data: async (id, data) => {
          return ObjectsGlobal.set.column(
              Notifications.meta.endpoint,
              id,
              "data",
              data
          )
        },
      },
      get: {
        all: async () => {
          return ObjectsGlobal.get.objects(Notifications.meta.endpoint);
        },
        single: async (id) => {
          return ObjectsGlobal.get.object(Notifications.meta.endpoint, parseInt(id));
        }
      },
      delete: async (id) => {
        return ObjectsGlobal.delete.object(Notifications.meta.endpoint, parseInt(id));
      },
      functions: {
        showDeleteObjectForm: (id, onAfterSubmit = null) => {
          return ObjectsGlobal.showDeleteObjectForm(Notifications, id, onAfterSubmit);
        },
        getNotificationCount: async (user_id = null) => {
          return authenticatedRequest(
              Notifications.meta.endpoint + "/count" + (user_id ? "?user_id=" + user_id : ""),
              "GET"
          );
        },
        markAsRead: async (id) => {
          return authenticatedRequest(
              Notifications.meta.endpoint + "?id=" + id,
              "DELETE"
          );
        },
      },
      /**
       * Show the create object form
       * @param onAfterSubmit
       * @param lockedValues
       * @returns {Promise<SweetAlertResult<Awaited<any>>>}
       */
      showCreateObjectForm: (onAfterSubmit = null, lockedValues = {}) => {
        return ObjectsGlobal.showCreateObjectForm(Notifications, 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(
            Notifications,
            id,
            column,
            value,
            onAfterSubmit
        );
      }
    };
</script>