pragma Singleton

import Quickshell
import QtQuick
import ZShell.Config

Singleton {
	id: root

	readonly property date date: clock.date
	readonly property string dateStr: format(Config.general.dateFormat)
	property alias enabled: clock.enabled
	readonly property string hourStr: timeComponents[0] ?? ""
	readonly property int hours: clock.hours
	readonly property string minuteStr: timeComponents[1] ?? ""
	readonly property int minutes: clock.minutes
	readonly property string secondStr: timeComponents[2] ?? ""
	readonly property int seconds: clock.seconds
	readonly property list<string> timeComponents: timeStr.split(":")
	readonly property string timeStr: format("hh:mm:ss")

	function format(fmt: string): string {
		return Qt.formatDateTime(clock.date, resolveFormat(fmt));
	}

	function resolveFormat(fmt: string): string {
		if (!Config.services.useTwelveHourClock)
			return fmt;

		let f = fmt.replace(/hh/g, "h");

		if (/h/.test(f) && !/AP|ap/.test(f))
			f += " AP";

		return f;
	}

	SystemClock {
		id: clock

		precision: SystemClock.Seconds
	}
}
