import {
  require_zod_to_json_schema
} from "./chunk-QSUS3GJF.js";
import {
  demoStepsSchema
} from "./chunk-JF6FAOTF.js";
import {
  base_command_default
} from "./chunk-LUHAPAT6.js";
import {
  AbortError,
  fileExists,
  mkdir,
  outputContent,
  outputSuccess,
  outputToken,
  readFile,
  renderAutocompletePrompt,
  writeFile
} from "./chunk-LA4HX5WI.js";
import {
  cwd,
  joinPath,
  resolvePath
} from "./chunk-T2GATXSP.js";
import {
  require_lib
} from "./chunk-CBD6UXLA.js";
import {
  __toESM,
  init_cjs_shims
} from "./chunk-POZ5MGPT.js";

// src/cli/commands/demo/generate-file.ts
init_cjs_shims();
var import_zod_to_json_schema = __toESM(require_zod_to_json_schema(), 1), import_core = __toESM(require_lib(), 1);
import { createRequire } from "module";
var schemaFilename = "demo-schema.json", GenerateFile = class _GenerateFile extends base_command_default {
  static {
    this.description = "Create a command design file";
  }
  static {
    this.summary = "Creates a JSON file alongside a JSON schema that will validate it";
  }
  static {
    this.hidden = !0;
  }
  static {
    this.flags = {
      path: import_core.Flags.string({
        hidden: !1,
        description: "The directory for generating the demo file.",
        env: "SHOPIFY_FLAG_PATH",
        parse: async (input) => resolvePath(input),
        default: async () => cwd()
      }),
      file: import_core.Flags.string({
        hidden: !1,
        description: "The name of the demo file.",
        env: "SHOPIFY_FLAG_FILENAME",
        required: !0,
        validate: (input) => input === schemaFilename ? `The demo file can't be named ${schemaFilename}, as this is used for the schema file.` : !0
      })
    };
  }
  async run() {
    let { flags } = await this.parse(_GenerateFile);
    await mkdir(flags.path);
    let demoFilePath = joinPath(flags.path, flags.file);
    if (await fileExists(demoFilePath))
      throw new AbortError(`The file ${demoFilePath} already exists.`);
    let demoSchemaPath = joinPath(flags.path, schemaFilename), jsonSchema = import_zod_to_json_schema.default.default(demoStepsSchema, "demo-steps");
    await Promise.all([
      writeFile(demoSchemaPath, JSON.stringify(jsonSchema, null, 2)),
      writeFile(
        demoFilePath,
        JSON.stringify(
          {
            $schema: `./${schemaFilename}`,
            steps: await selectSteps()
          },
          null,
          2
        )
      )
    ]), outputSuccess(outputContent`Created ${outputToken.path(demoFilePath)} and ${outputToken.path(demoSchemaPath)}`);
  }
};
async function selectSteps() {
  let catalogFile = createRequire(import.meta.url).resolve("@shopify/cli/assets/demo-catalog.json"), { steps } = JSON.parse(await readFile(catalogFile)), selectedSteps = [];
  for (; ; ) {
    let stepSelection = await renderAutocompletePrompt({
      message: "Add a step to the demo file",
      choices: [
        {
          label: "I'm done",
          value: "done"
        },
        ...steps.map(({ title, type }) => ({
          label: title,
          value: title,
          group: type
        }))
      ]
    });
    if (stepSelection === "done")
      break;
    selectedSteps.push(steps.find(({ title }) => title === stepSelection));
  }
  return selectedSteps;
}

export {
  GenerateFile
};
//# sourceMappingURL=chunk-HJ7ZFTP7.js.map
