import { computed } from "vue";
import { usePage } from "@inertiajs/vue3";

export function usePermissions() {
  const page = usePage();
  const permissions = computed<string[]>(
    () =>
      ((page.props.auth as Record<string, unknown>)?.permissions as string[]) ??
      [],
  );

  function can(permission: string | null | undefined): boolean {
    if (!permission) {
      return true;
    }

    return (
      permissions.value.includes("*") || permissions.value.includes(permission)
    );
  }

  return { can, permissions };
}
