import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  extensionHandlers,
  extensionTypeSchema
} from "./chunk-QRT2GK6X.js";
import "./chunk-Y5UQRKYO.js";
import "./chunk-7LH6HYMZ.js";
import "./chunk-SR7TTQEG.js";
import "./chunk-2ABOQGMV.js";
import "./chunk-TQF2C25T.js";
import "./chunk-M6WJXTS4.js";
import "./chunk-RF4NA2OH.js";
import "./chunk-NMCF7XG5.js";
import "./chunk-MHV22UIU.js";
import "./chunk-RDF2RA4E.js";
import "./chunk-AAUVYJJT.js";
import "./chunk-K6JLY75P.js";
import "./chunk-WFQIXGJV.js";
import "./chunk-ECBO3UFC.js";
import "./chunk-YNEGRO3E.js";
import {
  createHttpClient,
  isLoggedInToAccount
} from "./chunk-HWT3LUJJ.js";
import "./chunk-TOBTRYZF.js";
import "./chunk-7WVNO7VU.js";
import "./chunk-RPTAJN5Y.js";
import "./chunk-YL4347CI.js";
import "./chunk-TODKQSCF.js";
import "./chunk-AFPDHQYI.js";
import "./chunk-HYKBBZLS.js";
import "./chunk-C5RTKQVL.js";
import "./chunk-SVUXJRFD.js";
import "./chunk-YT5BPQDJ.js";
import "./chunk-LU3R3EBR.js";
import "./chunk-XZUGLEBK.js";
import "./chunk-VASURI4S.js";
import "./chunk-SDLCFZRL.js";
import {
  CliError,
  CliErrorCode
} from "./chunk-MTT3QXBC.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-LUTOVD5E.js.map