import { Name } from "@repo/rdx-ddd"; import { Maybe, Result } from "@repo/rdx-utils"; import { ProcessError, type ProcessTemplateInput, ProcessTemplateInputSchema, } from "../process-values"; export class ProcessTemplateValues { private constructor( public readonly name: Name, public readonly kind: ProcessTemplateInput["kind"], public readonly active: boolean, public readonly notes: Maybe, public readonly instructions: readonly Name[] ) { Object.freeze(this); } static create(input: ProcessTemplateInput): Result { const parsed = ProcessTemplateInputSchema.safeParse(input); if (!parsed.success) { return Result.fail(new ProcessError("INVALID_REQUEST")); } const name = Name.create(parsed.data.name, 160); if (!name.isSuccess) { return Result.fail(new ProcessError("INVALID_REQUEST")); } const instructions: Name[] = []; for (const value of parsed.data.instructions) { const item = Name.create(value, 160); if (!item.isSuccess) { return Result.fail(new ProcessError("INVALID_REQUEST")); } instructions.push(item.data); } return Result.ok( new ProcessTemplateValues( name.data, parsed.data.kind, parsed.data.active, parsed.data.notes ? Maybe.some(parsed.data.notes) : Maybe.none(), Object.freeze(instructions) ) ); } equals(other: ProcessTemplateValues) { return ( this.name.equals(other.name) && this.kind === other.kind && this.active === other.active && this.notes.getOrUndefined() === other.notes.getOrUndefined() && this.instructions.length === other.instructions.length && this.instructions.every((item, index) => item.equals(other.instructions[index])) ); } toPrimitive(): ProcessTemplateInput { return { name: this.name.toPrimitive(), kind: this.kind, active: this.active, notes: this.notes.getOrUndefined() ?? null, instructions: this.instructions.map((n) => n.toPrimitive()), }; } }