import { isNullishOrEmpty } from "./utils"; // Tri-estado para PATCH: unset | set(Some) | set(None) export class PatchField { private constructor( private readonly _isSet: boolean, private readonly _value?: T | null ) {} static unset(): PatchField { return new PatchField(false); } static set(value: T | null): PatchField { return new PatchField(true, value); } get isSet(): boolean { return this._isSet; } /** Devuelve el valor crudo (puede ser null) si isSet=true */ get value(): T | null | undefined { return this._value; } /** Ejecuta una función solo si isSet=true */ ifSet(fn: (v: T | null) => void): void { if (this._isSet) fn(this._value ?? null); } } export function toPatchField(value: T | null | undefined): PatchField { if (value === undefined) return PatchField.unset(); // "" => null if (isNullishOrEmpty(value)) return PatchField.set(null); return PatchField.set(value as T); }