import { Variable, GLib } from "astal";
import { Widget } from "astal/gtk4";

type Props = { 
    format: string, 
    interval?: number
} & Widget.LabelProps;

export default function DateTime({ format, interval, ...props }: Props) {
    const shouldPoll = typeof interval === "number" && interval >= 1;

    const currentTime = () => {
        const dateTime = GLib.DateTime.new_now_local();
        return dateTime.format(format)!;
    }

    if (shouldPoll) {
        const pollTime = new Variable(currentTime()).poll(interval || 1000, currentTime);
        return <label label={pollTime()} {...props} />
    } else {
        const time = new Variable(currentTime())
        return <label label={time()} {...props} />
    }
}