import * as wmill from "https://deno.land/x/windmill@v1.14.3/index.ts";
import { Octokit } from "https://cdn.skypack.dev/octokit?dts";
import { WebClient } from "https://deno.land/x/slack_web_api@1.0.3/mod.ts";

export async function main(
    gh_auth: wmill.Resource<"github">,
    owner: string,
    repo: string,
    channel: string,
    username: string
) {
    const octokit = new Octokit(gh_auth);
    const response = await octokit.rest.repos.get({
        owner,
        repo,
    });

    let repo_list = {};
    repo_list[response.data.name] = response.data.stargazers_count;
    const before = await wmill.getInternalState();
    const key = Object.keys(repo_list)[0];

    await wmill.setInternalState({ [key]: repo_list[key] });
    const after = await wmill.getInternalState();
    const before_val: number = Object.values(before)[0];
    const after_val: number = Object.values(after)[0];

    let notification_list = [];
    let notification: string =
        `No new Github stars on the following repo: \n ${key} : ${before_val} Stars`;
    if (after_val > before_val) {
        const difference: number = after_val - before_val;
        notification =
            `You have received ${difference} new Github stars on the following repo:\n${key} `;
        notification_list.push(notification);
    }

    return notification_list;
}