import { UserKeyDefinition, UserKeyDefinitionOptions } from "../../platform/state"; // eslint-disable-next-line -- `StateDefinition` used as a type import type { StateDefinition } from "../../platform/state/state-definition"; import { ClassifiedFormat } from "./classified-format"; import { Classifier } from "./classifier"; /** A key for storing JavaScript objects (`{ an: "example" }`) * in a UserStateSubject. */ // FIXME: promote to class: `ObjectConfiguration`. // The class receives `encryptor`, `prepareNext`, `adjust`, and `fix` // From `UserStateSubject`. `UserStateSubject` keeps `classify` and // `declassify`. The class should also include serialization // facilities (to be used in place of JSON.parse/stringify) in it's // options. Also allow swap between "classifier" and "classification"; the // latter is a list of properties/arguments to the specific classifier in-use. export type ObjectKey> = { target: "object"; key: string; state: StateDefinition; classifier: Classifier; format: "plain" | "classified"; options: UserKeyDefinitionOptions; initial?: State; }; export function isObjectKey(key: any): key is ObjectKey { return key.target === "object" && "format" in key && "classifier" in key; } export function toUserKeyDefinition( key: ObjectKey, ) { if (key.format === "plain") { const plain = new UserKeyDefinition(key.state, key.key, key.options); return plain; } else if (key.format === "classified") { const classified = new UserKeyDefinition>( key.state, key.key, { cleanupDelayMs: key.options.cleanupDelayMs, deserializer: (jsonValue) => jsonValue as ClassifiedFormat, clearOn: key.options.clearOn, }, ); return classified; } else { throw new Error(`unknown format: ${key.format}`); } }