{"version":3,"file":"rule.cjs","sourceRoot":"","sources":["../src/rule.ts"],"names":[],"mappings":";;;AA6FA,MAAa,QAAQ;IAGnB,YAAY,SAAyC;QACnD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED;;;;;OAKG;IACH,2BAA2B,CAAC,KAAyB;QACnD,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACjC,+BAA+B;QAC/B,kEAAkE;QAClE,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAClB,CAAC;QAEF,OAAO,OAAO,EAAE,QAAQ,CAAC,IAAI,IAAI,EAAE,CAAC;IACtC,CAAC;IAED;;;;;OAKG;IACH,6DAA6D;IAC7D,4BAA4B,CAAC,MAA2B;QACtD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAjCD,4BAiCC","sourcesContent":["import type {\n  AccountGroupType,\n  AccountWalletType,\n} from '@metamask/account-api';\nimport type {\n  AccountGroupIdOf,\n  AccountWalletIdOf,\n} from '@metamask/account-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { AccountGroupObject, AccountGroupObjectOf } from './group';\nimport type { AccountTreeControllerMessenger } from './types';\nimport type { AccountWalletObject, AccountWalletObjectOf } from './wallet';\n\nexport type RuleResult<\n  WalletType extends AccountWalletType,\n  GroupType extends AccountGroupType,\n> = {\n  wallet: {\n    type: WalletType;\n    id: AccountWalletIdOf<WalletType>;\n    // Omit `name` since it will get computed after the tree is built.\n    metadata: Omit<AccountWalletObjectOf<WalletType>['metadata'], 'name'>;\n  };\n  group: {\n    type: GroupType;\n    id: AccountGroupIdOf<WalletType>;\n    // Omit `name` since it will get computed after the tree is built.\n    metadata: Omit<AccountGroupObjectOf<GroupType>['metadata'], 'name'>;\n  };\n};\n\nexport type Rule<\n  WalletType extends AccountWalletType,\n  GroupType extends AccountGroupType,\n> = {\n  /**\n   * Account wallet type for this rule.\n   */\n  readonly walletType: WalletType;\n\n  /**\n   * Account group type for this rule.\n   */\n  readonly groupType: GroupType;\n\n  /**\n   * Applies the rule and check if the account matches.\n   *\n   * If the account matches, then the rule will return a {@link RuleResult} which means\n   * this account needs to be grouped within a wallet associated with this rule.\n   *\n   * If a wallet already exists for this account (based on {@link RuleResult}) then\n   * the account will be added to that wallet instance into its proper group (different for\n   * every wallets).\n   *\n   * @param account - The account to match.\n   * @returns A {@link RuleResult} if this account is part of that rule/wallet, returns\n   * `undefined` otherwise.\n   */\n  match(\n    account: InternalAccount,\n  ): RuleResult<WalletType, GroupType> | undefined;\n\n  /**\n   * Gets default name for a wallet.\n   *\n   * @param wallet - Wallet associated to this rule.\n   * @returns The default name for that wallet.\n   */\n  getDefaultAccountWalletName(\n    wallet: AccountWalletObjectOf<WalletType>,\n  ): string;\n\n  /**\n   * Gets computed name for a group based on its accounts.\n   *\n   * @param group - Group associated to this rule.\n   * @returns The computed name based on existing accounts.\n   */\n  getComputedAccountGroupName(group: AccountGroupObjectOf<GroupType>): string;\n\n  /**\n   * Gets default name for a group based on its position in the wallet.\n   *\n   * @param wallet - Wallet associated to this rule.\n   * @returns The default name prefix for groups of that wallet.\n   */\n  getDefaultAccountGroupPrefix(\n    wallet: AccountWalletObjectOf<WalletType>,\n  ): string;\n};\n\nexport class BaseRule {\n  protected readonly messenger: AccountTreeControllerMessenger;\n\n  constructor(messenger: AccountTreeControllerMessenger) {\n    this.messenger = messenger;\n  }\n\n  /**\n   * Gets computed name for a group based on its accounts.\n   *\n   * @param group - Group associated to this rule.\n   * @returns The computed name based on existing accounts.\n   */\n  getComputedAccountGroupName(group: AccountGroupObject): string {\n    const account = this.messenger.call(\n      'AccountsController:getAccount',\n      // Type-wise, we are guaranteed to always have at least 1 account.\n      group.accounts[0],\n    );\n\n    return account?.metadata.name ?? '';\n  }\n\n  /**\n   * Gets default prefix name for a group.\n   *\n   * @param wallet - Wallet of this group.\n   * @returns The default prefix name for that group.\n   */\n  // eslint-disable-next-line @typescript-eslint/no-unused-vars\n  getDefaultAccountGroupPrefix(wallet: AccountWalletObject): string {\n    return 'Account';\n  }\n}\n"]}