// @bun
import {
  isJsonSchema,
  isValidIdentifier,
  isZuiSchema
} from "./chunk-gspjbr79.js";
import {
  transforms_exports,
  uniq_default
} from "./chunk-wesykvkp.js";

// ../../node_modules/.bun/llmz@0.0.79+ed0f774122d294fe/node_modules/llmz/dist/chunk-E5YW5YQN.js
var Exit = class _Exit {
  name;
  aliases = [];
  description;
  metadata;
  schema;
  get zSchema() {
    return this.schema ? transforms_exports.fromJSONSchemaLegacy(this.schema) : undefined;
  }
  rename(name) {
    const before = this.name;
    if (!isValidIdentifier(name)) {
      throw new Error(`Invalid name for exit ${name}. An exit name must start with a letter and contain only letters, numbers, and underscores. It must be 1-50 characters long.`);
    }
    this.name = name;
    this.aliases = uniq_default([name, ...this.aliases.map((alias) => alias === before ? name : alias)]);
    return this;
  }
  clone() {
    return new _Exit({
      name: this.name,
      aliases: [...this.aliases],
      description: this.description,
      metadata: JSON.parse(JSON.stringify(this.metadata)),
      schema: this.zSchema
    });
  }
  is(exit) {
    return this.name === exit.name;
  }
  match(result) {
    return result.exit instanceof _Exit && this.name === result.exit.name;
  }
  toJSON() {
    return {
      name: this.name,
      aliases: [...this.aliases],
      description: this.description,
      metadata: { ...this.metadata },
      schema: this.schema
    };
  }
  constructor(props) {
    if (!isValidIdentifier(props.name)) {
      throw new Error(`Invalid name for exit ${props.name}. A exit name must start with a letter and contain only letters, numbers, and underscores. It must be 1-50 characters long.`);
    }
    if (typeof props.description !== "string" || props.description.trim().length === 0) {
      throw new Error(`Invalid description for exit ${props.name}. Expected a non-empty string, but got type "${typeof props.description}"`);
    }
    if (props.metadata !== undefined && typeof props.metadata !== "object") {
      throw new Error(`Invalid metadata for exit ${props.name}. Expected an object, but got type "${typeof props.metadata}"`);
    }
    if (props.aliases !== undefined && !Array.isArray(props.aliases)) {
      throw new Error(`Invalid aliases for exit ${props.name}. Expected an array, but got type "${typeof props.aliases}"`);
    }
    if (props.aliases && props.aliases.some((alias) => !isValidIdentifier(alias))) {
      throw new Error(`Invalid aliases for exit ${props.name}. Expected an array of valid identifiers.`);
    }
    if (typeof props.schema !== "undefined") {
      if (isZuiSchema(props.schema)) {
        this.schema = transforms_exports.toJSONSchemaLegacy(props.schema);
      } else if (isJsonSchema(props.schema)) {
        this.schema = props.schema;
      } else {
        throw new Error(`Invalid input schema for exit ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.schema}"`);
      }
    }
    this.name = props.name;
    this.aliases = uniq_default([props.name, ...props.aliases ?? []]);
    this.description = props.description;
    this.metadata = props.metadata ?? {};
  }
  static withUniqueNames = (exits) => {
    const names = /* @__PURE__ */ new Set;
    return exits.map((exit) => {
      if (exits.filter((t) => t.name === exit.name).length === 1) {
        return exit;
      }
      let counter = 1;
      let exitName = exit.name + counter;
      while (names.has(exitName)) {
        exitName = `${exit.name}${++counter}`;
      }
      return exit.rename(exitName);
    });
  };
};

export { Exit };
