import {schema} from "../schemata/SchemaData.js"; export enum SleepQuality { ACIDIC = "Acidic", RESTLESS = "Restless", NIGHTMARISH = "Nightmarish", TIMESKIP = "Timeskip", DREAMLESS = "Dreamless", DREAMY = "Dreamy", ECSTASY = "Ecstasy", } export const SLEEP_QUALITIES: SleepQuality[] = [SleepQuality.ACIDIC, SleepQuality.RESTLESS, SleepQuality.NIGHTMARISH, SleepQuality.TIMESKIP, SleepQuality.DREAMLESS, SleepQuality.DREAMY, SleepQuality.ECSTASY] export type SleepQualityJTD = typeof SleepQualityJTD export const SleepQualityJTD = schema({ schema: { enum: SLEEP_QUALITIES }, key: "sleepQuality", references: [], }) export enum WakeQuality { AGONIZED = "Agonized", EXHAUSTED = "Exhausted", DROWSY = "Drowsy", RESTED = "Rested", ENERGIZED = "Energized", } export const WAKE_QUALITIES: WakeQuality[] = [WakeQuality.AGONIZED, WakeQuality.EXHAUSTED, WakeQuality.DROWSY, WakeQuality.RESTED, WakeQuality.ENERGIZED] export type WakeQualityJTD = typeof WakeQualityJTD export const WakeQualityJTD = schema({ schema: { enum: WAKE_QUALITIES }, key: "wakeQuality", references: [], }) export interface SleepRecord { readonly sleepAt?: Date readonly sleepQuality?: SleepQuality readonly interruptions?: number readonly wakeAt?: Date readonly wakeQuality?: WakeQuality readonly dreams?: string } export type SleepRecordJTD = typeof SleepRecordJTD export const SleepRecordJTD = schema({ schema: { optionalProperties: { sleepAt: { type: "timestamp" }, sleepQuality: SleepQualityJTD.reference, interruptions: { type: "uint32" }, wakeAt: { type: "timestamp" }, wakeQuality: WakeQualityJTD.reference, dreams: { type: "string" }, } }, typeHint: null as SleepRecord|null, key: "sleepRecord", references: [SleepQualityJTD, WakeQualityJTD], }) export type SleepRecordListJTD = typeof SleepRecordListJTD export const SleepRecordListJTD = schema({ schema: { elements: SleepRecordJTD.reference }, typeHint: null as (readonly SleepRecord[])|null, key: "sleepRecords", references: [SleepRecordJTD, ...SleepRecordJTD.requiredReferences], }) const TimeFormat = Intl.DateTimeFormat([], { dateStyle: undefined, timeStyle: undefined, year: undefined, month: undefined, day: undefined, weekday: undefined, hour: "numeric", minute: "2-digit", second: undefined, fractionalSecondDigits: undefined, timeZone: undefined, hour12: true, hourCycle: "h12", }) export function sleepRecordToString(record: SleepRecord) { const sleepAt = record.sleepAt === undefined ? null : TimeFormat.format(record.sleepAt) const wakeAt = record.wakeAt === undefined ? null : TimeFormat.format(record.wakeAt) return `${sleepAt ?? "?:??"} (${record.sleepQuality ?? "???"}) -${record.interruptions ?? "?"}-> ${wakeAt ?? "?:??"} (${record.wakeQuality ?? "???"})${typeof record.dreams === "string" ? " with dream journal" : ""}` }