<script lang="ts">
  import type { TaskResult } from "../models/task";
  import Progress from "./Progress.svelte";

  interface ProjectLike {
    tasks?: TaskResult[]
    statusTag?: string
    subtractAmount?: number,
  }

  export let project: ProjectLike;

  let progressAmount: number;
  let progressTarget: number;

  $: {
    progressAmount = (project.tasks||[]).map(t => (t.active || t.statusTag === "to do" || t.statusTag === "on hold") 
      ? Math.min(t.completedAmount, t.itemAmount) * (t.item.groupWeight || 1) 
      : t.itemAmount * (t.item.groupWeight || 1)
    ).reduce((n,m) => n+m, 0);
    progressTarget = Math.max((project.tasks||[]).map(t => t.itemAmount * (t.item.groupWeight || 1)).reduce((n,m) => n+m, 0), 1);

    progressAmount = Math.max(progressAmount - (project.subtractAmount||0), 0);
    progressTarget = Math.max(progressTarget - (project.subtractAmount||0), 0);
  }
</script>

<Progress thin contextColor count={progressAmount} target={progressTarget} />
