import { type ZodType, type TypeOf,nativeEnum, boolean, number, string, array, record, unknown, lazy } from 'zod' import { AttachmentSchema } from './attachment' import { LabelSchema } from './label' import { UserSchema } from './user' import { SubscriptionSchema } from './subscription' import { PRIORITIES } from '@/constants/priorities' import { RepeatsSchema } from './common/repeats' import { DateSchema } from './common/date' import { IdSchema } from './common/id' import { HexColorSchema } from './common/hexColor' import { AbstractSchema } from './abstract' import { TextFieldSchema } from './common/textField' import { TASK_REPEAT_MODES } from '@/types/IRepeatMode' import { RelationKindSchema } from './common/RelationKind' const LabelsSchema = array(LabelSchema) .transform((labels) => labels.sort((f, s) => f.title > s.title ? 1 : -1)) // FIXME: use .default([]) export type ILabels = TypeOf const RelatedTasksSchema = record(RelationKindSchema, record(string(), unknown())) export type IRelatedTasksSchema = TypeOf const RelatedTasksLazySchema : ZodType = lazy(() => record(RelationKindSchema, TaskSchema), ) export type IRelatedTasksLazySchema = TypeOf export const TaskSchema = AbstractSchema.extend({ id: IdSchema.default(0), title: TextFieldSchema, description: TextFieldSchema, done: boolean().default(false), doneAt: DateSchema.nullable().default(null), priority: nativeEnum(PRIORITIES).default(PRIORITIES.UNSET), labels: LabelsSchema, assignees: array(UserSchema).default([]), dueDate: DateSchema.nullable(), // FIXME: default value is `0`. Shouldn't this be `null`? startDate: DateSchema.nullable(), // FIXME: default value is `0`. Shouldn't this be `null`? endDate: DateSchema.nullable(), // FIXME: default value is `0`. Shouldn't this be `null`? repeatAfter: RepeatsSchema, // FIXME: default value is `0`. Shouldn't this be `null`? repeatFromCurrentDate: boolean().default(false), repeatMode: nativeEnum(TASK_REPEAT_MODES).default(TASK_REPEAT_MODES.REPEAT_MODE_DEFAULT), // TODO: schedule notifications // FIXME: triggered notificaitons not supported anymore / remove feature? reminderDates: array(DateSchema).default([]), parentTaskId: IdSchema.default(0), // shouldn't this have `null` as default? hexColor: HexColorSchema.default(''), percentDone: number().default(0), relatedTasks: RelatedTasksSchema.default({}), attachments: array(AttachmentSchema).default([]), identifier: string().default(''), index: number().default(0), isFavorite: boolean().default(false), subscription: SubscriptionSchema.nullable().default(null), position: number().default(0), kanbanPosition: number().default(0), createdBy: UserSchema, created: DateSchema.nullable(), updated: DateSchema.nullable(), listId: IdSchema.default(0), //IList['id'], // Meta, only used when creating a new task bucketId: IdSchema.default(0), // IBucket['id'], }).transform((obj) => { if (obj.identifier === `-${obj.index}`) { obj.identifier = '' } return obj }) export type Task = TypeOf