import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  extensionHandlers,
  extensionTypeSchema
} from "./chunk-H5RUH62H.js";
import "./chunk-H5GIJ5WD.js";
import "./chunk-FZ3LMCL3.js";
import "./chunk-SR7TTQEG.js";
import "./chunk-D5GYKQIG.js";
import "./chunk-TQF2C25T.js";
import "./chunk-6GQQXLEJ.js";
import "./chunk-KUSCYV4K.js";
import "./chunk-NMCF7XG5.js";
import "./chunk-MHV22UIU.js";
import "./chunk-BYP4JZFS.js";
import "./chunk-AAUVYJJT.js";
import "./chunk-K6JLY75P.js";
import "./chunk-BRX4HWJE.js";
import "./chunk-ZBFOAJ5H.js";
import "./chunk-YNEGRO3E.js";
import {
  createHttpClient,
  isLoggedInToAccount
} from "./chunk-YHFCDWGT.js";
import "./chunk-IVQPYJ5M.js";
import "./chunk-MDUBZZTI.js";
import "./chunk-RPTAJN5Y.js";
import "./chunk-QK47PIWK.js";
import "./chunk-TODKQSCF.js";
import "./chunk-AFPDHQYI.js";
import "./chunk-HYKBBZLS.js";
import "./chunk-C5RTKQVL.js";
import "./chunk-DGJS6KZB.js";
import "./chunk-YT5BPQDJ.js";
import "./chunk-LU3R3EBR.js";
import "./chunk-XZUGLEBK.js";
import "./chunk-AYCGCYN3.js";
import "./chunk-SDLCFZRL.js";
import {
  CliError,
  CliErrorCode
} from "./chunk-WGV2XQSB.js";
import {
  init_esm_shims
} from "./chunk-EXLZF52D.js";

// ../cli-astro-commands/src/non-interactive/generate-extensions.ts
init_esm_shims();
import { relative } from "node:path";
async function assertAuth() {
  const authData = await isLoggedInToAccount();
  if (!authData) {
    throw new CliError({
      code: CliErrorCode.AuthenticationRequired(),
      cause: null
    });
  }
  return authData;
}
function parseExtensionType(params) {
  const extensionTypeResult = extensionTypeSchema.safeParse(params);
  if (!extensionTypeResult.success) {
    throw new Error(
      `'extensionType' must be specified. Run 'wix schema generate' to see available types.`
    );
  }
  const { extensionType } = extensionTypeResult.data;
  if (!Object.hasOwn(extensionHandlers, extensionType)) {
    throw new Error(
      `Unknown extension type: '${extensionType}'. Run 'wix schema generate' to see available types.`
    );
  }
  return extensionType;
}
async function nonInteractiveGenerateExtension(_services, model, params) {
  const authData = await assertAuth();
  const extensionType = parseExtensionType(params);
  const handler = extensionHandlers[extensionType];
  const httpClient = createHttpClient({
    type: "backoffice",
    getAppToken: () => authData.getAccessToken({})
  });
  const zodSchema = typeof handler.zodSchema === "function" ? await handler.zodSchema(httpClient) : handler.zodSchema;
  const result = zodSchema.safeParse(params);
  if (!result.success) {
    throw new Error(
      result.error.errors.map(
        (e) => e.path.length > 0 ? `${e.path.join(".")}: ${e.message}` : e.message
      ).join("\n")
    );
  }
  if (handler.validate) {
    const validationError = await handler.validate(
      model.projectFolder,
      result.data
    );
    if (validationError) {
      throw new Error(validationError);
    }
  }
  const generationResult = await handler.generate({
    projectFolder: model.projectFolder,
    appId: model.config.appId,
    model,
    data: result.data,
    httpClient
  });
  return {
    extensionType,
    newFiles: (generationResult?.newFiles ?? []).map(
      (f) => relative(model.projectFolder, f)
    )
  };
}
export {
  nonInteractiveGenerateExtension
};
//# sourceMappingURL=generate-extensions-N5IUWBUQ.js.map