import { array, safeParse, string } from "valibot";
import { adminLog, betterFetch, log } from "./utils";
import { Client } from "discord.js";

let client: Client;
export const setClient = (clnt: Client) => client = clnt;

export let lidiCoMajDenimPremium: string[] = [];

const subscriptionsSchema = array(string());

function naplanovat() {
	const datum = new Date();

	datum.setHours(24);
	datum.setMinutes(1);
	datum.setSeconds(0);

	setTimeout(sync, Number(datum) - Date.now());
}

async function sync() {
	const res = await betterFetch("https://util.deadfish.cz/denim+/subscriptions.json")
		.then(r => r.json())
		.catch(e => log("chyba pri ziskavani denim- predplatitelu", e));

	const result = safeParse(subscriptionsSchema, res);

	if (result.success) {
		lidiCoMajDenimPremium = result.output;
	} else {
		adminLog(client, "nepovedlo se akutalizovat vlastnitele denim-");
	}

	naplanovat();
}

sync();
