"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var generators_exports = {};
__export(generators_exports, {
  jsonSchemaToTypescriptZuiSchema: () => jsonSchemaToTypescriptZuiSchema,
  primitiveRecordToRecordString: () => primitiveRecordToRecordString,
  primitiveRecordToTypescriptValues: () => primitiveRecordToTypescriptValues,
  primitiveToTypescriptValue: () => primitiveToTypescriptValue,
  stringifySingleLine: () => stringifySingleLine,
  typescriptValuesToRecordString: () => typescriptValuesToRecordString,
  zuiSchemaToTypeScriptType: () => zuiSchemaToTypeScriptType
});
module.exports = __toCommonJS(generators_exports);
var sdk = __toESM(require("@botpress/sdk"));
var import_lodash = __toESM(require("lodash"));
var prettier = __toESM(require("prettier"));
var utils = __toESM(require("../utils"));
var consts = __toESM(require("./consts"));
const zuiSchemaToTypeScriptType = async (zuiSchema, name) => {
  let code = zuiSchema.toTypescriptType({ treatDefaultAsOptional: true });
  code = `export type ${name} = ${code}`;
  code = await prettier.format(code, { parser: "typescript" });
  return [
    //
    consts.GENERATED_HEADER,
    code
  ].join("\n");
};
const jsonSchemaToTypescriptZuiSchema = async (schema, name, extraProps = {}) => {
  schema = await utils.schema.dereferenceSchema(schema);
  const zuiSchema = sdk.transforms.fromJSONSchemaLegacy(schema);
  const allProps = {
    ...extraProps,
    schema: zuiSchema.toTypescriptSchema()
  };
  let code = [
    consts.GENERATED_HEADER,
    'import { z } from "@botpress/sdk"',
    `export const ${name} = ${typescriptValuesToRecordString(allProps)}`
  ].join("\n");
  code = await prettier.format(code, { parser: "typescript" });
  return code;
};
const stringifySingleLine = (x) => {
  return JSON.stringify(x, null, 1).replace(/\n */g, " ");
};
function primitiveToTypescriptValue(x) {
  if (typeof x === "undefined") {
    return "undefined";
  }
  return JSON.stringify(x);
}
function primitiveRecordToTypescriptValues(x) {
  return (0, import_lodash.default)(x).toPairs().filter(([_key, value]) => value !== void 0).map(([key, value]) => [key, primitiveToTypescriptValue(value)]).fromPairs().value();
}
const primitiveRecordToRecordString = (record) => typescriptValuesToRecordString(primitiveRecordToTypescriptValues(record));
const typescriptValuesToRecordString = (record) => ["{", ...Object.entries(record).map(([key, value]) => `  ${key}: ${value},`), "}"].join("\n");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  jsonSchemaToTypescriptZuiSchema,
  primitiveRecordToRecordString,
  primitiveRecordToTypescriptValues,
  primitiveToTypescriptValue,
  stringifySingleLine,
  typescriptValuesToRecordString,
  zuiSchemaToTypeScriptType
});
//# sourceMappingURL=generators.js.map
