9 } from '@proton/pass/types';
10 import { CardType, type PlatformSpecific } from '@proton/pass/types/protobuf/item-v1';
11 import { type ObjectHandler, objectHandler } from '@proton/pass/utils/object/handler';
12 import { uniqueId } from '@proton/pass/utils/string/unique-id';
14 import { deobfuscateItem, obfuscateItem } from './item.obfuscation';
16 export const itemMetaFactory = (): ObjectHandler<Metadata> =>
17 objectHandler({ name: '', note: '', itemUuid: uniqueId() });
19 export const itemContentBuilder = <T extends ItemType, R = ObjectHandler<UnsafeItemContent<T>>>(type: T): R => {
22 return objectHandler<UnsafeItemContent<'alias'>>({}) as R;
25 return objectHandler<UnsafeItemContent<'creditCard'>>({
27 cardType: CardType.Unspecified,
29 verificationNumber: '',
35 return objectHandler<UnsafeItemContent<'login'>>({
45 return objectHandler<UnsafeItemContent<'note'>>({}) as R;
48 return objectHandler<UnsafeItemContent<'identity'>>({
65 socialSecurityNumber: '',
75 secondPhoneNumber: '',
81 extraAddressDetails: [],
83 extraContactDetails: [],
85 extraPersonalDetails: [],
90 throw new Error('unsupported item type');
93 type ItemBuilderInterface<T extends ItemType = ItemType> = {
96 content: ObjectHandler<UnsafeItemContent<K>>;
97 metadata: ObjectHandler<Metadata>;
98 extraFields: UnsafeItemExtraField[];
99 platformSpecific?: PlatformSpecific;
103 export const itemBuilder = <T extends ItemType>(type: T, from?: Item<T>) => {
104 const init = (from ? deobfuscateItem(from as Item) : null) as MaybeNull<UnsafeItem>;
106 return objectHandler<ItemBuilderInterface<T>, Item<T>>(
109 content: itemContentBuilder<T>(type).merge(init?.content ?? {}),
110 extraFields: init?.extraFields ?? [],
111 metadata: itemMetaFactory().merge(init?.metadata ?? {}),
112 platformSpecific: init?.platformSpecific,
117 content: item.content.data,
118 metadata: item.metadata.data,
123 export type ItemBuilder<T extends ItemType> = ObjectHandler<ItemBuilderInterface<T>, Item<T>>;