import { computed } from 'vue';
import { useRoute } from 'vue-router';
import { OBJECT_CONTEXT_QUERY_KEY } from '@/constants/object-context';
import { useObjectContextStore } from '@/store/modules/object-context';
import { normalizeCurrentProductSummary, resolveObjectIdFromQuery } from './product-context-shared';

export function useCurrentProduct() {
  const route = useRoute();
  const objectContextStore = useObjectContextStore();

  const currentObjectId = computed(() => {
    return resolveObjectIdFromQuery(route.query[OBJECT_CONTEXT_QUERY_KEY], objectContextStore.objectId);
  });

  const currentProduct = computed(() =>
    normalizeCurrentProductSummary(objectContextStore.objectSummary, objectContextStore.objectName)
  );

  return {
    currentObjectId,
    currentProduct
  };
}
