import { Serializable, SERIALIZABLE_CLASS_MAP } from "./serializable.ts";
import { SerializePropertyOptionsMap } from "./serialize_property_options_map.ts";import { FromJSONStrategy, ToJSONStrategy,} from "./strategy/compose_strategy.ts";import { ERROR_SYMBOL_PROPERTY_NAME } from "./error_messages.ts";
export class SerializePropertyOptions { public fromJSONStrategy?: FromJSONStrategy; public toJSONStrategy?: ToJSONStrategy;
constructor( public propertyKey: string | symbol, public serializedKey: string, fromJSONStrategy?: FromJSONStrategy, toJSONStrategy?: ToJSONStrategy, ) { if (fromJSONStrategy) { this.fromJSONStrategy = fromJSONStrategy; }
if (toJSONStrategy) { this.toJSONStrategy = toJSONStrategy; } }}
export type ToSerializedKeyStrategy = (propertyName: string) => string;
export type SerializePropertyArgument = | string | ToSerializedKeyStrategy | { serializedKey?: string | ToSerializedKeyStrategy; fromJSONStrategy?: FromJSONStrategy; toJSONStrategy?: ToJSONStrategy; };
interface SerializePropertyArgumentObject { serializedKey: string; fromJSONStrategy?: FromJSONStrategy; toJSONStrategy?: ToJSONStrategy;}
export function SerializeProperty( args?: string | SerializePropertyArgument,): PropertyDecorator { return ( target: unknown, propertyName: string | symbol, ) => { const decoratorArguments = args ?? {}; const decoratorArgumentOptions = getDecoratorArgumentOptions( decoratorArguments, target, propertyName, );
let serializablePropertiesMap = SERIALIZABLE_CLASS_MAP.get(target);
if (!serializablePropertiesMap) { const parentMap = SERIALIZABLE_CLASS_MAP.get( Object.getPrototypeOf(target), );
SERIALIZABLE_CLASS_MAP.set( target, new SerializePropertyOptionsMap(parentMap), );
serializablePropertiesMap = SERIALIZABLE_CLASS_MAP.get( target, ); }
serializablePropertiesMap?.set( new SerializePropertyOptions( propertyName, decoratorArgumentOptions.serializedKey, decoratorArgumentOptions.fromJSONStrategy, decoratorArgumentOptions.toJSONStrategy, ), ); };}
function getDecoratorArgumentOptions( decoratorArguments: SerializePropertyArgument, target: unknown, propertyName: string | symbol,): SerializePropertyArgumentObject { if (typeof decoratorArguments === "string") { return { serializedKey: decoratorArguments }; }
if (typeof decoratorArguments === "function") { return { serializedKey: decoratorArguments(String(propertyName)), }; }
if ( !decoratorArguments.serializedKey && typeof propertyName === "symbol" ) { throw new Error(ERROR_SYMBOL_PROPERTY_NAME); }
if (typeof decoratorArguments.serializedKey === "function") { return { serializedKey: decoratorArguments.serializedKey(String(propertyName)), fromJSONStrategy: decoratorArguments.fromJSONStrategy, toJSONStrategy: decoratorArguments.toJSONStrategy, }; }
if (typeof decoratorArguments.serializedKey === "string") { return decoratorArguments as SerializePropertyArgumentObject; }
return { serializedKey: (target as Serializable).tsTransformKey( String(propertyName), ), fromJSONStrategy: decoratorArguments.fromJSONStrategy, toJSONStrategy: decoratorArguments.toJSONStrategy, };}