// import Helper from '@ember/component/helper'; import { capabilities, setHelperManager } from '@ember/helper'; import { decorate } from '../-private/decorator.ts'; // import { getOwner } from '../-private/-owner'; import { createCommandInstance } from '../-private/instance.ts'; import type { Commandable, CommandInstance } from '../-private/instance.ts'; import type Owner from '@ember/owner'; import type { HelperCapabilities } from '@glimmer/interfaces'; // eslint-disable-next-line @typescript-eslint/no-explicit-any type NonInstanceType = K extends InstanceType ? object : K; type DecoratorKey = K extends string | symbol ? K : never; interface Args { positional: unknown; named: object; } type CommandHelperSignature = (owner: Owner, ...commandables: Commandable[]) => CommandInstance; class CommandHelperManager { capabilities: HelperCapabilities = capabilities('3.23', { hasValue: true }); constructor(private owner: Owner | undefined) {} createHelper(fn: CommandHelperSignature, args: Args) { return { fn, args }; } getValue({ fn, args }: { fn: CommandHelperSignature; args: Args }) { // eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style return fn(this.owner as Owner, ...(args.positional as Commandable[])); } } interface DecoratorPropertyDescriptor extends PropertyDescriptor { initializer?(): unknown; } export default function command(...commandables: Commandable[]): CommandInstance; /** * The `@use` decorator can be used to use a Resource in javascript classes * * ```js * import { command } from 'ember-resources'; * * class MyC { * @command data = Clock; * } * * ``` */ export default function command( prototype: NonInstanceType, key: DecoratorKey, descriptor?: DecoratorPropertyDescriptor ): void; // commandables: Commandable[] | object, // key?: string | symbol | Owner, // desc?: DecoratorPropertyDescriptor export default function command( ...args: Parameters | [Owner, ...Commandable[]] | Commandable[] ): void | CommandInstance { if ( args.length === 3 && ((args[2] as DecoratorPropertyDescriptor).get || (args[2] as DecoratorPropertyDescriptor).initializer) ) { return decorate( args[0], args[1] as string | symbol, args[2] as DecoratorPropertyDescriptor ) as unknown as void; } return createCommandInstance(args[0] as Owner, args.slice(1) as Commandable[]); // if (key && desc && (desc.get || desc.initializer)) { // return decorate(undefined, key as string | symbol, desc) as unknown as void; // } // return createCommandInstance(key as Owner, commandables as Commandable[]); } setHelperManager((owner) => new CommandHelperManager(owner as Owner), command);