import {
  AuthenticatedCommand
} from "./chunk-G2JISDKX.js";
import {
  require_lib
} from "./chunk-XECLOG6B.js";
import {
  require_source
} from "./chunk-M5OAS5QZ.js";
import {
  __toESM,
  init_cjs_shims
} from "./chunk-7GH3YGSC.js";

// src/commands/migrate-ledger-entry.ts
init_cjs_shims();
var import_core = __toESM(require_lib(), 1);
var import_chalk = __toESM(require_source(), 1);
var MigrateLedgerEntry = class extends AuthenticatedCommand {
  static {
    this.summary = "Migrate a Ledger Entry to a new type and version.";
  }
  static {
    this.description = `This mirrors the ${import_chalk.default.magentaBright(
      "migrateLedgerEntry"
    )} mutation in the GraphQL API.

  Migrates an existing Ledger Entry to a new type and version with new parameters.
  `;
  }
  static {
    this.examples = ["<%= config.bin %> <%= command.id %>"];
  }
  static {
    this.flags = {
      "entry.id": import_core.Flags.string({
        char: "e",
        description: "The ID of the Ledger Entry to migrate.",
        required: true
      }),
      "ledger.ik": import_core.Flags.string({
        char: "l",
        description: "The Idempotency Key of the Ledger containing the entry.",
        required: true
      }),
      "new.entry.type": import_core.Flags.string({
        char: "t",
        required: true,
        description: "The new type for the migrated Ledger Entry."
      }),
      "new.entry.type.version": import_core.Flags.integer({
        char: "n",
        required: false,
        description: "The new version of the Ledger Entry type."
      }),
      param: import_core.Flags.string({
        char: "p",
        multiple: true,
        summary: "The parameters for the new Ledger Entry. (ex: --param amount=100 --param currency=USD)",
        description: "You may specify multiple parameters by repeating the flag."
      })
    };
  }
  async run() {
    const parameters = this.flags.param?.reduce((acc, param) => {
      const [key, value] = param.split("=");
      if (!key || !value) {
        this.logToStderr(
          `Invalid value for parameters. ${import_chalk.default.cyan(
            "`parameters`"
          )} must be in the format of key=value. You can specify multiple parameters by repeating the flag.
          
You provided:
${import_chalk.default.yellow(param)}`
        );
        this.exit(1);
      }
      if (acc[key]) {
        this.logToStderr(
          `Invalid value for parameters. ${import_chalk.default.cyan(
            "`parameters`"
          )} must not contain duplicate keys.
          
You provided:
${import_chalk.default.yellow(param)}`
        );
        this.exit(1);
      }
      acc[key] = value;
      return acc;
    }, {});
    const res = await this.sdk.MigrateLedgerEntry({
      input: {
        id: this.flags["entry.id"],
        newLedgerEntry: {
          ledger: {
            ik: this.flags["ledger.ik"]
          },
          type: this.flags["new.entry.type"],
          typeVersion: this.flags["new.entry.type.version"],
          parameters
        }
      }
    });
    if (res.migrateLedgerEntry.__typename !== "MigrateLedgerEntryResult") {
      this.logToStderr(`Received error: ${res.migrateLedgerEntry.__typename}`);
      this.logToStderr(`Message: ${res.migrateLedgerEntry.message}`);
      this.logToStderr(`Code: ${res.migrateLedgerEntry.code}`);
      this.logToStderr(`Retryable: ${res.migrateLedgerEntry.retryable}`);
      this.exit(1);
    }
  }
};

export {
  MigrateLedgerEntry
};
