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

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

/**
 * The Roles object
 */
export const Roles = {
  meta: {
    title: "Brugerroller",
    icon: "fas fa-list",
    description: "Oversigt over brugerroller",
    endpoint: "/roles",
    labels: {
      single: "brugerrolle",
      multiple: "brugerroller"
    }
  },
  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: true
      }
    },
  },
  add: async (name, description) => {
    return ObjectsGlobal.add.object(
        Roles.meta.endpoint,
        {
          name: name,
          description: description
        }
    );
  },
  set: {
    name: async (id, name) => {
      return ObjectsGlobal.set.column(
        Roles.meta.endpoint,
        id,
        "name",
        name
      )
    },
    description: async (id, description) => {
      return ObjectsGlobal.set.column(
        Roles.meta.endpoint,
        id,
        "description",
        description
      )
    },
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(Roles.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(Roles.meta.endpoint, id);
    }
  },
  functions: {
    getRoleName: getRoleName,
    showDeleteObjectForm: (id, onAfterSubmit = null) => {
      return ObjectsGlobal.showDeleteObjectForm(Roles, id, onAfterSubmit);
    },
    /**
     * Show the clone object form
     * @param id
     */
    cloneObject: async (id) => {
      return authenticatedRequest("/roles/clone", "POST", {
        id: id
      });
    },
    permissions: {
      add: async (roleId, permissionId) => {
        return authenticatedRequest("/roles/permissions", "POST", {
          group_id: parseInt(roleId),
          permission_id: permissionId
        });
      },
      remove: async (roleId, permissionId) => {
        return authenticatedRequest("/roles/permissions?group_id=" + roleId + "&permission_id=" + permissionId, "DELETE");
      }
    }
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null) => {
    return ObjectsGlobal.showCreateObjectForm(Roles, 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(
        Roles,
        id,
        column,
        value,
        onAfterSubmit
    );
  }
};
</script>