massarg 1.0.7-pre.1 → 2.0.0-pre.1

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.
package/command.js ADDED
@@ -0,0 +1,317 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MassargHelpCommand = exports.MassargCommand = exports.CommandConfig = void 0;
4
+ const zod_1 = require("zod");
5
+ const error_1 = require("./error");
6
+ const help_1 = require("./help");
7
+ const option_1 = require("./option");
8
+ const utils_1 = require("./utils");
9
+ const example_1 = require("./example");
10
+ const CommandConfig = (args) => zod_1.z.object({
11
+ /** Command name */
12
+ name: zod_1.z.string(),
13
+ /** Command description, displayed in the help output */
14
+ description: zod_1.z.string(),
15
+ /** Command aliases */
16
+ aliases: zod_1.z.string().array().optional(),
17
+ /**
18
+ * Function used when invoking this command. It receives the parsed options and the primary
19
+ * instance of Massarg used to invoke this command (the top-level instance)
20
+ */
21
+ run: zod_1.z
22
+ .function()
23
+ .args(args, zod_1.z.any())
24
+ .returns(zod_1.z.union([zod_1.z.promise(zod_1.z.void()), zod_1.z.void()])),
25
+ });
26
+ exports.CommandConfig = CommandConfig;
27
+ /**
28
+ * A command is a named function that can be invoked with a set of options.
29
+ *
30
+ * Commands can have sub-commands, which can have their own sub-commands, and so on.
31
+ *
32
+ * Options are not inherited by sub-commands, but their parsed values are passed down when
33
+ * invoking a sub-command. This works recursively.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * massarg(options).command({
38
+ * name: 'foo',
39
+ * description: 'foo command',
40
+ * run: (options, instance) => {
41
+ * console.log(options, instance)
42
+ * },
43
+ * })
44
+ * ```
45
+ */
46
+ class MassargCommand {
47
+ constructor(options, parent) {
48
+ this.commands = [];
49
+ this.options = [];
50
+ this.examples = [];
51
+ this.args = {};
52
+ (0, exports.CommandConfig)(zod_1.z.any()).parse(options);
53
+ this.name = options.name;
54
+ this.description = options.description;
55
+ this.aliases = options.aliases ?? [];
56
+ this._run = options.run;
57
+ this._helpConfig = {};
58
+ this.parent = parent;
59
+ }
60
+ get helpConfig() {
61
+ if (this.parent) {
62
+ return (0, utils_1.deepMerge)(this.parent.helpConfig, this._helpConfig);
63
+ }
64
+ return (0, utils_1.deepMerge)(help_1.defaultHelpConfig, this._helpConfig);
65
+ }
66
+ command(config) {
67
+ try {
68
+ const command = config instanceof MassargCommand ? config : new MassargCommand(config);
69
+ const existing = this.commands.find((c) => c.name === command.name);
70
+ if (existing) {
71
+ throw new error_1.ValidationError({
72
+ code: 'duplicate_command',
73
+ message: `Command "${command.name}" already exists`,
74
+ path: [this.name, command.name],
75
+ });
76
+ }
77
+ command.parent = this;
78
+ this.commands.push(command);
79
+ return this;
80
+ }
81
+ catch (e) {
82
+ if ((0, error_1.isZodError)(e)) {
83
+ throw new error_1.ValidationError({
84
+ path: [this.name, config.name, ...e.issues[0].path.map((p) => p.toString())],
85
+ code: e.issues[0].code,
86
+ message: e.issues[0].message,
87
+ });
88
+ }
89
+ throw e;
90
+ }
91
+ }
92
+ flag(config) {
93
+ try {
94
+ const flag = config instanceof option_1.MassargFlag ? config : new option_1.MassargFlag(config);
95
+ const existing = this.options.find((c) => c.name === flag.name);
96
+ if (existing) {
97
+ throw new error_1.ValidationError({
98
+ code: 'duplicate_flag',
99
+ message: `Flag "${flag.name}" already exists`,
100
+ path: [this.name, flag.name],
101
+ });
102
+ }
103
+ this.options.push(flag);
104
+ return this;
105
+ }
106
+ catch (e) {
107
+ if ((0, error_1.isZodError)(e)) {
108
+ throw new error_1.ValidationError({
109
+ path: [this.name, config.name, ...e.issues[0].path.map((p) => p.toString())],
110
+ code: e.issues[0].code,
111
+ message: e.issues[0].message,
112
+ });
113
+ }
114
+ throw e;
115
+ }
116
+ }
117
+ option(config) {
118
+ try {
119
+ const option = config instanceof option_1.MassargOption ? config : option_1.MassargOption.fromTypedConfig(config);
120
+ const existing = this.options.find((c) => c.name === option.name);
121
+ if (existing) {
122
+ throw new error_1.ValidationError({
123
+ code: 'duplicate_option',
124
+ message: `Option "${option.name}" already exists`,
125
+ path: [this.name, option.name],
126
+ });
127
+ }
128
+ if (option.isDefault) {
129
+ const defaultOption = this.options.find((o) => o.isDefault);
130
+ if (defaultOption) {
131
+ throw new error_1.ValidationError({
132
+ code: 'duplicate_default_option',
133
+ message: `Option "${option.name}" cannot be set as default because option "${defaultOption.name}" is already set as default`,
134
+ path: [this.name, option.name],
135
+ });
136
+ }
137
+ }
138
+ this.options.push(option);
139
+ return this;
140
+ }
141
+ catch (e) {
142
+ if ((0, error_1.isZodError)(e)) {
143
+ throw new error_1.ValidationError({
144
+ path: [this.name, config.name, ...e.issues[0].path.map((p) => p.toString())],
145
+ code: e.issues[0].code,
146
+ message: e.issues[0].message,
147
+ });
148
+ }
149
+ throw e;
150
+ }
151
+ }
152
+ /**
153
+ * Adds an example to this command.
154
+ *
155
+ * An example is a description of how to use the command, with an example input and output.
156
+ *
157
+ * At least one of `description`, `input` or `output` must be provided, but neither alone is
158
+ * required.
159
+ */
160
+ example(config) {
161
+ this.examples.push(new example_1.MassargExample(config));
162
+ return this;
163
+ }
164
+ /**
165
+ * Configure the help output for this (and all child) commands.
166
+ *
167
+ * You can automatically bind the help command to this command, and/or bind the help option
168
+ * to this command.
169
+ *
170
+ * If you don't opt-in to this behavior with `bindCommand` or `bindOption`, you can still
171
+ * access the help output via `this.helpString()` and `this.printHelp()`.
172
+ */
173
+ help(config) {
174
+ this._helpConfig = help_1.HelpConfig.parse(config);
175
+ if (this.helpConfig.bindCommand) {
176
+ this.command(new MassargHelpCommand());
177
+ }
178
+ if (this.helpConfig.bindOption) {
179
+ this.option(new option_1.MassargHelpFlag());
180
+ }
181
+ return this;
182
+ }
183
+ /**
184
+ * Configure the main function for this command. This command will run when no sub-commands
185
+ * are provided.
186
+ *
187
+ * If none is provided, help will be printed.
188
+ */
189
+ main(run) {
190
+ this._run = run;
191
+ return this;
192
+ }
193
+ /**
194
+ * Parse the given arguments and run the command or sub-commands along with the given options
195
+ * and flags.
196
+ *
197
+ * To parse the arguments without running any commands and only get the output args,
198
+ * use `getArgs` instead.
199
+ */
200
+ parse(argv, args, parent) {
201
+ this.getArgs(argv, args, parent, true);
202
+ }
203
+ parseOption(arg, argv) {
204
+ const option = this.options.find((o) => o._match(arg));
205
+ if (!option) {
206
+ throw new error_1.ValidationError({
207
+ path: [option_1.MassargOption.getName(arg)],
208
+ code: 'unknown_option',
209
+ message: 'Unknown option',
210
+ });
211
+ }
212
+ const res = option._parseDetails([arg, ...argv]);
213
+ this.args[res.key] = (0, utils_1.setOrPush)(res.value, this.args[res.key], option.isArray);
214
+ return res.argv;
215
+ }
216
+ getArgs(argv, args, parent, parseCommands = false) {
217
+ let _args = { ...this.args, ...args };
218
+ let _argv = [...argv];
219
+ const _a = this.args;
220
+ // fill defaults
221
+ for (const option of this.options) {
222
+ if (option.defaultValue !== undefined && _a[option.name] === undefined) {
223
+ _args[option.name] = option.defaultValue;
224
+ }
225
+ }
226
+ // parse options
227
+ while (_argv.length) {
228
+ const arg = _argv.shift();
229
+ // make sure option exists
230
+ const found = this.options.some((o) => o._isOption(arg));
231
+ if (found) {
232
+ _argv = this.parseOption(arg, _argv);
233
+ _args = { ..._args, ...this.args };
234
+ continue;
235
+ }
236
+ // if not, try see if it's a command
237
+ const command = this.commands.find((c) => c.name === arg || c.aliases.includes(arg));
238
+ if (command) {
239
+ // this is dry run, just exit
240
+ if (!parseCommands) {
241
+ break;
242
+ }
243
+ // this is real run, parse command, pass unparsed args
244
+ return command.parse(_argv, this.args, parent ?? this);
245
+ }
246
+ // default option - passes arg value even without flag name
247
+ const defaultOption = this.options.find((o) => o.isDefault);
248
+ if (defaultOption) {
249
+ _argv = this.parseOption(`--${defaultOption.name}`, [arg, ..._argv]);
250
+ continue;
251
+ }
252
+ // not parsed by any step, add to extra key
253
+ _a.extra ??= [];
254
+ _a.extra.push(arg);
255
+ }
256
+ // merge args
257
+ this.args = { ...this.args, ..._args };
258
+ // dry run, just exit
259
+ if (!parseCommands) {
260
+ return this.args;
261
+ }
262
+ // no sub command found, run main command
263
+ if (this._run) {
264
+ this._run(this.args, parent ?? this);
265
+ }
266
+ }
267
+ /**
268
+ * Generate the help output for this command, and return it as a string.
269
+ */
270
+ helpString() {
271
+ return new help_1.HelpGenerator(this).generate();
272
+ }
273
+ /**
274
+ * Print the help output for this command.
275
+ */
276
+ printHelp() {
277
+ console.log(this.helpString());
278
+ }
279
+ }
280
+ exports.MassargCommand = MassargCommand;
281
+ class MassargHelpCommand extends MassargCommand {
282
+ constructor(config = {}) {
283
+ super({
284
+ name: 'help',
285
+ aliases: ['h'],
286
+ description: 'Print help for this command, or a subcommand if specified',
287
+ // argsHint: "[command]",
288
+ run: (args, parent) => {
289
+ if (args.command) {
290
+ const command = parent.commands.find((c) => c.name === args.command);
291
+ if (command) {
292
+ command.printHelp();
293
+ return;
294
+ }
295
+ else {
296
+ throw new error_1.ParseError({
297
+ path: ['command'],
298
+ code: 'unknown_command',
299
+ message: 'Unknown command',
300
+ received: args.command,
301
+ });
302
+ }
303
+ }
304
+ parent.printHelp();
305
+ },
306
+ ...config,
307
+ });
308
+ this.option({
309
+ name: 'command',
310
+ aliases: ['c'],
311
+ description: 'Command to print help for',
312
+ isDefault: true,
313
+ });
314
+ }
315
+ }
316
+ exports.MassargHelpCommand = MassargHelpCommand;
317
+ //# sourceMappingURL=command.js.map
package/command.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.js","sourceRoot":"","sources":["../src/command.ts"],"names":[],"mappings":";;;AAAA,6BAAuB;AACvB,mCAAiE;AACjE,iCAAqE;AACrE,qCAMiB;AACjB,mCAA4D;AAC5D,uCAAyD;AAElD,MAAM,aAAa,GAAG,CAA4B,IAAa,EAAE,EAAE,CACxE,OAAC,CAAC,MAAM,CAAC;IACP,mBAAmB;IACnB,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE;IAChB,wDAAwD;IACxD,WAAW,EAAE,OAAC,CAAC,MAAM,EAAE;IACvB,s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AAA;YACpB,CAAC;YACD,GAAG,MAAM;SACV,CAAC,CAAA;QACF,IAAI,CAAC,MAAM,CAAC;YACV,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,CAAC,GAAG,CAAC;YACd,WAAW,EAAE,2BAA2B;YACxC,SAAS,EAAE,IAAI;SAChB,CAAC,CAAA;IACJ,CAAC;CACF;AAjCD,gDAiCC"}
package/error.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { z } from 'zod';
2
+ /** This error is thrown when a validation fails. */
3
+ export declare class ValidationError extends Error {
4
+ /** The path to the value that failed validation. */
5
+ path: string[];
6
+ /** The error code. */
7
+ code: string;
8
+ /** The error message. */
9
+ message: string;
10
+ constructor({ path, code, message }: {
11
+ path: string[];
12
+ code: string;
13
+ message: string;
14
+ });
15
+ }
16
+ /** This error is thrown when a parse fails on an option value. */
17
+ export declare class ParseError extends Error {
18
+ /** The path to the value that failed parsing. */
19
+ path: string[];
20
+ /** The error code. */
21
+ code: string;
22
+ /** The error message. */
23
+ message: string;
24
+ /** The value that failed parsing. */
25
+ received: unknown;
26
+ constructor({ path, code, message, received, }: {
27
+ path: string[];
28
+ code: string;
29
+ message: string;
30
+ received?: unknown;
31
+ });
32
+ }
33
+ export declare function isZodError(e: unknown): e is z.ZodError;
34
+ //# sourceMappingURL=error.d.ts.map
package/error.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,oDAAoD;AACpD,qBAAa,eAAgB,SAAQ,KAAK;IACxC,oDAAoD;IACpD,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,yBAAyB;IACzB,OAAO,EAAE,MAAM,CAAA;gBAEH,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,EAAE,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;CAQvF;AAED,kEAAkE;AAClE,qBAAa,UAAW,SAAQ,KAAK;IACnC,iDAAiD;IACjD,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,yBAAyB;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,qCAAqC;IACrC,QAAQ,EAAE,OAAO,CAAA;gBAEL,EACV,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,QAAQ,GACT,EAAE;QACD,IAAI,EAAE,MAAM,EAAE,CAAA;QACd,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,QAAQ,CAAC,EAAE,OAAO,CAAA;KACnB;CAYF;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAEtD"}
package/error.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isZodError = exports.ParseError = exports.ValidationError = void 0;
4
+ const zod_1 = require("zod");
5
+ /** This error is thrown when a validation fails. */
6
+ class ValidationError extends Error {
7
+ constructor({ path, code, message }) {
8
+ const msg = `${path.join('.')}: ${message}`;
9
+ super(msg);
10
+ this.path = path;
11
+ this.code = code;
12
+ this.message = msg;
13
+ this.name = 'ValidationError';
14
+ }
15
+ }
16
+ exports.ValidationError = ValidationError;
17
+ /** This error is thrown when a parse fails on an option value. */
18
+ class ParseError extends Error {
19
+ constructor({ path, code, message, received, }) {
20
+ let msg = `${path.join('.')}: ${message}`;
21
+ if (received) {
22
+ msg += ` (received: ${received})`;
23
+ }
24
+ super(msg);
25
+ this.path = path;
26
+ this.code = code;
27
+ this.message = msg;
28
+ this.name = 'ParseError';
29
+ this.received = received;
30
+ }
31
+ }
32
+ exports.ParseError = ParseError;
33
+ function isZodError(e) {
34
+ return e instanceof zod_1.z.ZodError;
35
+ }
36
+ exports.isZodError = isZodError;
37
+ //# sourceMappingURL=error.js.map
package/error.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;AAAA,6BAAuB;AAEvB,oDAAoD;AACpD,MAAa,eAAgB,SAAQ,KAAK;IAQxC,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAqD;QACpF,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,OAAO,EAAE,CAAA;QAC3C,KAAK,CAAC,GAAG,CAAC,CAAA;QACV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,OAAO,GAAG,GAAG,CAAA;QAClB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAA;IAC/B,CAAC;CACF;AAhBD,0CAgBC;AAED,kEAAkE;AAClE,MAAa,UAAW,SAAQ,KAAK;IAUnC,YAAY,EACV,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,QAAQ,GAMT;QACC,IAAI,GAAG,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,OAAO,EAAE,CAAA;QACzC,IAAI,QAAQ,EAAE;YACZ,GAAG,IAAI,eAAe,QAAQ,GAAG,CAAA;SAClC;QACD,KAAK,CAAC,GAAG,CAAC,CAAA;QACV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,OAAO,GAAG,GAAG,CAAA;QAClB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAA;QACxB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC1B,CAAC;CACF;AAhCD,gCAgCC;AAED,SAAgB,UAAU,CAAC,CAAU;IACnC,OAAO,CAAC,YAAY,OAAC,CAAC,QAAQ,CAAA;AAChC,CAAC;AAFD,gCAEC"}
package/example.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import z from 'zod';
2
+ export declare const ExampleConfig: z.ZodObject<{
3
+ /** Description of the example. This will appear as a title above the input/output line(s). */
4
+ description: z.ZodOptional<z.ZodString>;
5
+ /**
6
+ * Input of the example. This will appear as a line below the description, with a `$` prefix.
7
+ * The prefix can be changed using the `help()` function on the command.
8
+ */
9
+ input: z.ZodOptional<z.ZodString>;
10
+ /**
11
+ * Output of the example. This will appear as a line below the input, with a `>` prefix.
12
+ * The prefix can be changed using the `help()` function on the command.
13
+ */
14
+ output: z.ZodOptional<z.ZodString>;
15
+ }, "strip", z.ZodTypeAny, {
16
+ description?: string | undefined;
17
+ input?: string | undefined;
18
+ output?: string | undefined;
19
+ }, {
20
+ description?: string | undefined;
21
+ input?: string | undefined;
22
+ output?: string | undefined;
23
+ }>;
24
+ export type ExampleConfig = z.infer<typeof ExampleConfig>;
25
+ export declare class MassargExample {
26
+ description: string | undefined;
27
+ input: string | undefined;
28
+ output: string | undefined;
29
+ constructor(config: ExampleConfig);
30
+ }
31
+ //# sourceMappingURL=example.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAGnB,eAAO,MAAM,aAAa;IACxB,8FAA8F;;IAE9F;;;OAGG;;IAEH;;;OAGG;;;;;;;;;;EAEH,CAAA;AACF,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAA;AAEzD,qBAAa,cAAc;IACzB,WAAW,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;gBAEd,MAAM,EAAE,aAAa;CAiBlC"}
package/example.js ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MassargExample = exports.ExampleConfig = void 0;
7
+ const zod_1 = __importDefault(require("zod"));
8
+ const error_1 = require("./error");
9
+ exports.ExampleConfig = zod_1.default.object({
10
+ /** Description of the example. This will appear as a title above the input/output line(s). */
11
+ description: zod_1.default.string().optional(),
12
+ /**
13
+ * Input of the example. This will appear as a line below the description, with a `$` prefix.
14
+ * The prefix can be changed using the `help()` function on the command.
15
+ */
16
+ input: zod_1.default.string().optional(),
17
+ /**
18
+ * Output of the example. This will appear as a line below the input, with a `>` prefix.
19
+ * The prefix can be changed using the `help()` function on the command.
20
+ */
21
+ output: zod_1.default.string().optional(),
22
+ });
23
+ class MassargExample {
24
+ constructor(config) {
25
+ exports.ExampleConfig.parse(config);
26
+ if (config.description === undefined &&
27
+ config.input === undefined &&
28
+ config.output === undefined) {
29
+ throw new error_1.ValidationError({
30
+ code: 'invalid_example',
31
+ message: 'Example must have at least one of description, input, or output',
32
+ path: ['example'],
33
+ });
34
+ }
35
+ this.description = config.description;
36
+ this.input = config.input;
37
+ this.output = config.output;
38
+ }
39
+ }
40
+ exports.MassargExample = MassargExample;
41
+ //# sourceMappingURL=example.js.map
package/example.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"example.js","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAmB;AACnB,mCAAyC;AAE5B,QAAA,aAAa,GAAG,aAAC,CAAC,MAAM,CAAC;IACpC,8FAA8F;IAC9F,WAAW,EAAE,aAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAClC;;;OAGG;IACH,KAAK,EAAE,aAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC5B;;;OAGG;IACH,MAAM,EAAE,aAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CAC9B,CAAC,CAAA;AAGF,MAAa,cAAc;IAKzB,YAAY,MAAqB;QAC/B,qBAAa,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;QAC3B,IACE,MAAM,CAAC,WAAW,KAAK,SAAS;YAChC,MAAM,CAAC,KAAK,KAAK,SAAS;YAC1B,MAAM,CAAC,MAAM,KAAK,SAAS,EAC3B;YACA,MAAM,IAAI,uBAAe,CAAC;gBACxB,IAAI,EAAE,iBAAiB;gBACvB,OAAO,EAAE,iEAAiE;gBAC1E,IAAI,EAAE,CAAC,SAAS,CAAC;aAClB,CAAC,CAAA;SACH;QACD,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAA;QACrC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAA;IAC7B,CAAC;CACF;AAtBD,wCAsBC"}