@shware/purchase 1.5.2 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -111,7 +111,8 @@ var AppStoreConfig = class _AppStoreConfig {
|
|
|
111
111
|
};
|
|
112
112
|
getCreditAmount = (productId) => {
|
|
113
113
|
const config = this.products.get(productId);
|
|
114
|
-
(0, import_utils.invariant)(config, `Product not found for ${productId}`);
|
|
114
|
+
(0, import_utils.invariant)(config !== void 0, `Product not found for ${productId}`);
|
|
115
|
+
(0, import_utils.invariant)(config !== null, `Credits not available for non-consumable product ${productId}`);
|
|
115
116
|
return config.credits;
|
|
116
117
|
};
|
|
117
118
|
getCreditExpiresIn = (productId) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms from 'ms';\nimport type { Metadata } from '../types';\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PL;\n readonly config: AppStoreConfig<NS, PE, PL, PI>;\n readonly products: Map<string, Metadata>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, PL, PI>, plan: PL) {\n this.plan = plan;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, PL, PI | K, I | K> => {\n this.products.set(productId, metadata);\n return this as Subscription<NS, PE, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = { appId: `${number}`; bundleId: string };\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n\n private products: Map<string, Metadata | null>;\n private consumables: Set<string>;\n private subscriptions: Map<PE, Subscription<NS, PE, PL, PI>>;\n private nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (subscription: Subscription<NS, PE, PL, PI>) => {\n subscription.products.forEach((metadata, productId) => this.products.set(productId, metadata));\n this.subscriptions.set(subscription.plan, subscription);\n return this;\n };\n\n private constructor(options: Options) {\n this.appId = options.appId;\n this.bundleId = options.bundleId;\n this.products = new Map();\n this.consumables = new Set();\n this.subscriptions = new Map();\n this.nonConsumables = new Set();\n }\n\n static create = <NS extends Lowercase<string>, PE extends string>(options: Options) => {\n return new AppStoreConfig<NS, PE>(options);\n };\n\n subscription = <K extends PE>(plan: K extends PL ? never : K) => {\n return new Subscription<NS, PE, K | PL, PI>(this, plan);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ) => {\n this.consumables.add(productId);\n this.products.set(productId, metadata);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n getPlan = (productId: string): PE => {\n for (const [plan, subscription] of this.subscriptions.entries()) {\n if (subscription.products.has(productId)) return plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n if (this.consumables.has(productId) || this.nonConsumables.has(productId)) return 'payment';\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return 'subscription';\n }\n throw new Error(`Mode not found for product ${productId}`);\n };\n\n getCreditAmount = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return config.credits;\n };\n\n private getCreditExpiresIn = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return ms(config.expiresIn);\n };\n\n getCreditExpiresAt = (productId: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAC1B,gBAAe;AAGf,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,eAAN,MAME;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEA,YAAY,QAAwC,MAAU;AAC5D,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,WAAW,oBAAI,IAAI;AACxB,SAAK,mBAAmB;AAAA,EAC1B;AAAA,EAEA,UAAU,CACR,WACA,WAAqB,EAAE,SAAS,GAAG,WAAW,IAAI,MACN;AAC5C,SAAK,SAAS,IAAI,WAAW,QAAQ;AACrC,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,qBAAwB;AACjC,SAAK,mBAAmB;AACxB,SAAK,OAAO,SAAS,EAAE,IAAa;AACpC,WAAO,KAAK;AAAA,EACd;AACF;AAIO,IAAM,iBAAN,MAAM,gBAKX;AAAA,EACS;AAAA,EACA;AAAA,EAED;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAER,IAAI,aAAuB;AACzB,WAAO,MAAM,KAAK,KAAK,SAAS,KAAK,CAAC;AAAA,EACxC;AAAA,EAEA,CAAC,SAAS,IAAI,CAAC,iBAA+C;AAC5D,iBAAa,SAAS,QAAQ,CAAC,UAAU,cAAc,KAAK,SAAS,IAAI,WAAW,QAAQ,CAAC;AAC7F,SAAK,cAAc,IAAI,aAAa,MAAM,YAAY;AACtD,WAAO;AAAA,EACT;AAAA,EAEQ,YAAY,SAAkB;AACpC,SAAK,QAAQ,QAAQ;AAC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.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms from 'ms';\nimport type { Metadata } from '../types';\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PL;\n readonly config: AppStoreConfig<NS, PE, PL, PI>;\n readonly products: Map<string, Metadata>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, PL, PI>, plan: PL) {\n this.plan = plan;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, PL, PI | K, I | K> => {\n this.products.set(productId, metadata);\n return this as Subscription<NS, PE, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = { appId: `${number}`; bundleId: string };\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n\n private products: Map<string, Metadata | null>;\n private consumables: Set<string>;\n private subscriptions: Map<PE, Subscription<NS, PE, PL, PI>>;\n private nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (subscription: Subscription<NS, PE, PL, PI>) => {\n subscription.products.forEach((metadata, productId) => this.products.set(productId, metadata));\n this.subscriptions.set(subscription.plan, subscription);\n return this;\n };\n\n private constructor(options: Options) {\n this.appId = options.appId;\n this.bundleId = options.bundleId;\n this.products = new Map();\n this.consumables = new Set();\n this.subscriptions = new Map();\n this.nonConsumables = new Set();\n }\n\n static create = <NS extends Lowercase<string>, PE extends string>(options: Options) => {\n return new AppStoreConfig<NS, PE>(options);\n };\n\n subscription = <K extends PE>(plan: K extends PL ? never : K) => {\n return new Subscription<NS, PE, K | PL, PI>(this, plan);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ) => {\n this.consumables.add(productId);\n this.products.set(productId, metadata);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n getPlan = (productId: string): PE => {\n for (const [plan, subscription] of this.subscriptions.entries()) {\n if (subscription.products.has(productId)) return plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n if (this.consumables.has(productId) || this.nonConsumables.has(productId)) return 'payment';\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return 'subscription';\n }\n throw new Error(`Mode not found for product ${productId}`);\n };\n\n getCreditAmount = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config !== undefined, `Product not found for ${productId}`);\n invariant(config !== null, `Credits not available for non-consumable product ${productId}`);\n return config.credits;\n };\n\n private getCreditExpiresIn = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return ms(config.expiresIn);\n };\n\n getCreditExpiresAt = (productId: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAC1B,gBAAe;AAGf,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,eAAN,MAME;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEA,YAAY,QAAwC,MAAU;AAC5D,SAAK,OAAO;AACZ,SA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|
78
78
|
getCreditAmount = (productId) => {
|
|
79
79
|
const config = this.products.get(productId);
|
|
80
|
-
invariant(config, `Product not found for ${productId}`);
|
|
80
|
+
invariant(config !== void 0, `Product not found for ${productId}`);
|
|
81
|
+
invariant(config !== null, `Credits not available for non-consumable product ${productId}`);
|
|
81
82
|
return config.credits;
|
|
82
83
|
};
|
|
83
84
|
getCreditExpiresIn = (productId) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms from 'ms';\nimport type { Metadata } from '../types';\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PL;\n readonly config: AppStoreConfig<NS, PE, PL, PI>;\n readonly products: Map<string, Metadata>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, PL, PI>, plan: PL) {\n this.plan = plan;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, PL, PI | K, I | K> => {\n this.products.set(productId, metadata);\n return this as Subscription<NS, PE, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = { appId: `${number}`; bundleId: string };\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n\n private products: Map<string, Metadata | null>;\n private consumables: Set<string>;\n private subscriptions: Map<PE, Subscription<NS, PE, PL, PI>>;\n private nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (subscription: Subscription<NS, PE, PL, PI>) => {\n subscription.products.forEach((metadata, productId) => this.products.set(productId, metadata));\n this.subscriptions.set(subscription.plan, subscription);\n return this;\n };\n\n private constructor(options: Options) {\n this.appId = options.appId;\n this.bundleId = options.bundleId;\n this.products = new Map();\n this.consumables = new Set();\n this.subscriptions = new Map();\n this.nonConsumables = new Set();\n }\n\n static create = <NS extends Lowercase<string>, PE extends string>(options: Options) => {\n return new AppStoreConfig<NS, PE>(options);\n };\n\n subscription = <K extends PE>(plan: K extends PL ? never : K) => {\n return new Subscription<NS, PE, K | PL, PI>(this, plan);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ) => {\n this.consumables.add(productId);\n this.products.set(productId, metadata);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n getPlan = (productId: string): PE => {\n for (const [plan, subscription] of this.subscriptions.entries()) {\n if (subscription.products.has(productId)) return plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n if (this.consumables.has(productId) || this.nonConsumables.has(productId)) return 'payment';\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return 'subscription';\n }\n throw new Error(`Mode not found for product ${productId}`);\n };\n\n getCreditAmount = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return config.credits;\n };\n\n private getCreditExpiresIn = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return ms(config.expiresIn);\n };\n\n getCreditExpiresAt = (productId: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n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,eAAe,CAAe,SAAmC;AAC/D,WAAO,IAAI,aAAiC,MAAM,IAAI;AAAA,EACxD;AAAA,EAEA,aAAa,CACX,WACA,WAAqB,EAAE,SAAS,GAAG,WAAW,IAAI,MAC/C;AACH,SAAK,YAAY,IAAI,SAAS;AAC9B,SAAK,SAAS,IAAI,WAAW,QAAQ;AACrC,WAAO;AAAA,EACT;AAAA,EAEA,gBAAgB,CAAyC,cAAwC;AAC/F,SAAK,eAAe,IAAI,SAAS;AACjC,SAAK,SAAS,IAAI,WAAW,IAAI;AACjC,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,cAA0B;AACnC,eAAW,CAAC,MAAM,YAAY,KAAK,KAAK,cAAc,QAAQ,GAAG;AAC/D,UAAI,aAAa,SAAS,IAAI,SAAS,EAAG,QAAO;AAAA,IACnD;AACA,UAAM,IAAI,MAAM,8BAA8B,SAAS,EAAE;AAAA,EAC3D;AAAA,EAEA,UAAU,CAAC,cAAkD;AAC3D,QAAI,KAAK,YAAY,IAAI,SAAS,KAAK,KAAK,eAAe,IAAI,SAAS,EAAG,QAAO;AAClF,eAAW,gBAAgB,KAAK,cAAc,OAAO,GAAG;AACtD,UAAI,aAAa,SAAS,IAAI,SAAS,EAAG,QAAO;AAAA,IACnD;AACA,UAAM,IAAI,MAAM,8BAA8B,SAAS,EAAE;AAAA,EAC3D;AAAA,EAEA,kBAAkB,CAAC,cAA8B;AAC/C,UAAM,SAAS,KAAK,SAAS,IAAI,SAAS;AAC1C,cAAU,
|
|
1
|
+
{"version":3,"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms from 'ms';\nimport type { Metadata } from '../types';\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PL;\n readonly config: AppStoreConfig<NS, PE, PL, PI>;\n readonly products: Map<string, Metadata>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, PL, PI>, plan: PL) {\n this.plan = plan;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, PL, PI | K, I | K> => {\n this.products.set(productId, metadata);\n return this as Subscription<NS, PE, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = { appId: `${number}`; bundleId: string };\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n\n private products: Map<string, Metadata | null>;\n private consumables: Set<string>;\n private subscriptions: Map<PE, Subscription<NS, PE, PL, PI>>;\n private nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (subscription: Subscription<NS, PE, PL, PI>) => {\n subscription.products.forEach((metadata, productId) => this.products.set(productId, metadata));\n this.subscriptions.set(subscription.plan, subscription);\n return this;\n };\n\n private constructor(options: Options) {\n this.appId = options.appId;\n this.bundleId = options.bundleId;\n this.products = new Map();\n this.consumables = new Set();\n this.subscriptions = new Map();\n this.nonConsumables = new Set();\n }\n\n static create = <NS extends Lowercase<string>, PE extends string>(options: Options) => {\n return new AppStoreConfig<NS, PE>(options);\n };\n\n subscription = <K extends PE>(plan: K extends PL ? never : K) => {\n return new Subscription<NS, PE, K | PL, PI>(this, plan);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ) => {\n this.consumables.add(productId);\n this.products.set(productId, metadata);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n getPlan = (productId: string): PE => {\n for (const [plan, subscription] of this.subscriptions.entries()) {\n if (subscription.products.has(productId)) return plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n if (this.consumables.has(productId) || this.nonConsumables.has(productId)) return 'payment';\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return 'subscription';\n }\n throw new Error(`Mode not found for product ${productId}`);\n };\n\n getCreditAmount = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config !== undefined, `Product not found for ${productId}`);\n invariant(config !== null, `Credits not available for non-consumable product ${productId}`);\n return config.credits;\n };\n\n private getCreditExpiresIn = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return ms(config.expiresIn);\n };\n\n getCreditExpiresAt = (productId: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,QAAQ;AAGf,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,eAAN,MAME;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEA,YAAY,QAAwC,MAAU;AAC5D,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,WAAW,oBAAI,IAAI;AACxB,SAAK,mBAAmB;AAAA,EAC1B;AAAA,EAEA,UAAU,CACR,WACA,WAAqB,EAAE,SAAS,GAAG,WAAW,IAAI,MACN;AAC5C,SAAK,SAAS,IAAI,WAAW,QAAQ;AACrC,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,qBAAwB;AACjC,SAAK,mBAAmB;AACxB,SAAK,OAAO,SAAS,EAAE,IAAa;AACpC,WAAO,KAAK;AAAA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