import { log_debug } from "../logger";
import { postAnalyticEvent } from "../manager";

import {
  replaceAnalyticsPropsNils,
  OfflineItemState,
  downloadsEventForState,
} from "./helper";

export const sendDownloadsEvent = ({
  item,
  state,
}: {
  item: ZappEntry;
  state: OfflineItemState;
}) => {
  const analyticsProps = replaceAnalyticsPropsNils({
    id: item?.id,
    title: item?.title,
    contentType: item?.content?.type,
  });

  const eventName = downloadsEventForState(state);

  if (!eventName) {
    // Ignored event state
    return;
  }

  postAnalyticEvent(eventName, analyticsProps);

  log_debug(`sendOnNavItemClickEvent: send event: ${eventName}`, {
    analyticsProps,
  });
};
