const projectPage = dv.page(input.file);
const tasks = projectPage.file.inlinks.where((p) => {
    const mt = dv.page(p.path)
    return mt.tags?.includes("process/task");
});

const totalTasksProjectPage = projectPage.file.tasks.length;
const totalTasksInTasks = tasks.values.reduce((acc, t) => {
    const mt = dv.page(t.path);
    return acc + mt.file.tasks.length;
}, 0);
const finishedTasksProjectPage = projectPage.file.tasks.where((t) => t.fullyCompleted === true).length;
const finishedTasksinTasks = tasks.values.reduce((acc, t) => {
    const mt = dv.page(t.path);
    return acc + mt.file.tasks.where((t) => t.fullyCompleted === true).length;
}, 0);

const ProjectTarget = totalTasksProjectPage + totalTasksInTasks;
const ProjectProgress = finishedTasksProjectPage + finishedTasksinTasks;

const containerEl = createDiv();
const align = input?.alignleft ? "left" : "center";
Object.assign(containerEl.style, {
    display: "flex",
    "flex-direction": "column",
    "align-items": align,
    "justify-content": "center",
});

const max = ProjectTarget || 0;
const value = ProjectProgress || 0;
const percent = Math.round((value / max) * 100) || 0;
const progressBar = containerEl.createEl("progress");
Object.assign(progressBar, { max, value });

const progressText = containerEl.createEl("div");
Object.assign(progressText, {
    textContent: `${percent}% completed`,
});

dv.paragraph(containerEl.innerHTML);
