import { computed } from 'vue';
import { useObjectContextStore } from '@/store/modules/object-context';

/**
 * 获取当前项目上下文
 */
export function useCurrentProject() {
  const objectContextStore = useObjectContextStore();

  const currentObjectId = computed(() => objectContextStore.objectId);

  const currentProject = computed(() => {
    const summary = objectContextStore.objectSummary;

    if (!summary) {
      return null;
    }

    return ((summary as unknown as Api.Project.ProjectContext).currentProject || null) as
      | Api.Project.ProjectContext['currentProject']
      | null;
  });

  const currentRole = computed(() => {
    const summary = objectContextStore.objectSummary;

    if (!summary) {
      return null;
    }

    return (summary as unknown as Api.Project.ProjectContext).currentRole || null;
  });

  const isGuest = computed(() => currentRole.value?.guestFlag ?? true);

  return {
    currentObjectId,
    currentProject,
    currentRole,
    isGuest
  };
}
