import * as md5 from "md5";
import { getActivities } from "../invokers/activities";
import { getAsset } from "../invokers/assets";
import { getScriptMetadata } from "../invokers/script";
import { activitiesStats, activitiesTimeline } from "../invokers/search";

export default {
  Package: {
    scriptMetadata: async ({ name, namespace }) =>
      getScriptMetadata.execute({
        package: { name, namespace },
      }),
    readme: async ({ readme: asset }) => getAsset.execute(asset),
    activities: async ({ name, namespace }) =>
      getActivities.execute({ id: md5((namespace || "none") + name) }),
    stats: async ({ name, namespace }) => {
      const {
        items: [stats],
      } = await activitiesStats.execute([{ name, namespace }]);

      return {
        // in case of a no statistics
        successful: 0,
        failed: 0,
        latestRelease: new Date().toISOString(),

        // overwrite default props
        ...(stats || {}),
      };
    },
    activitiesTimeline: async ({ name, namespace }) =>
      activitiesTimeline.execute({ name, namespace }),
  },
};
