{"version":3,"file":"iconv-DlFMt2gW.mjs","names":[],"sources":["../src/runtime/iconv.ts"],"sourcesContent":["/**\n * Character encoding conversion utilities.\n *\n * Thin typed wrapper around the platform-provided `tailor.iconv` runtime API.\n * At runtime this delegates to `globalThis.tailor.iconv`, which is provided by\n * the Tailor Platform Function runtime. Use `mockIconv` from\n * `@tailor-platform/sdk/vitest` to mock these calls in unit tests.\n * @example\n * import { iconv } from \"@tailor-platform/sdk/runtime\";\n *\n * const utf8 = iconv.convert(sjisBuffer, \"Shift_JIS\", \"UTF-8\"); // string\n * const sjis = iconv.convert(\"こんにちは\", \"UTF-8\", \"Shift_JIS\"); // Uint8Array\n *\n * const conv = new iconv.Iconv(\"Shift_JIS\", \"UTF-8\");\n * const out = conv.convert(sjisBuffer);\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/** Instance methods exposed by `tailor.iconv.Iconv`. */\nexport interface IconvInstance {\n  convert(input: string | Uint8Array | ArrayBuffer): string | Uint8Array;\n}\n\n/** Constructor shape for `tailor.iconv.Iconv`. */\nexport interface IconvConstructor {\n  new (fromEncoding: string, toEncoding: string): IconvInstance;\n}\n\n/**\n * Platform API surface for `tailor.iconv`. Describes the shape the platform\n * runtime injects on `globalThis.tailor.iconv` so the wrapper and ambient\n * globals stay in sync.\n */\nexport interface TailorIconvAPI {\n  /**\n   * Convert a string or buffer between encodings.\n   * @param str - Input data to convert\n   * @param fromEncoding - Source encoding name\n   * @param toEncoding - Target encoding name\n   * @returns `string` when `toEncoding` is `\"UTF8\"` or `\"UTF-8\"`, otherwise `Uint8Array`.\n   */\n  convert<T extends string>(\n    str: string | Uint8Array | ArrayBuffer,\n    fromEncoding: string,\n    toEncoding: T,\n  ): T extends \"UTF8\" | \"UTF-8\" ? string : Uint8Array;\n\n  /**\n   * Convert a buffer between encodings.\n   * @param buffer - Input bytes to convert\n   * @param fromEncoding - Source encoding name\n   * @param toEncoding - Target encoding name\n   * @returns `string` when `toEncoding` is `\"UTF8\"` or `\"UTF-8\"`, otherwise `Uint8Array`.\n   */\n  convertBuffer<T extends string>(\n    buffer: Uint8Array | ArrayBuffer,\n    fromEncoding: string,\n    toEncoding: T,\n  ): T extends \"UTF8\" | \"UTF-8\" ? string : Uint8Array;\n\n  /**\n   * Decode a buffer to a UTF-8 string using the given source encoding.\n   * @param buffer - Input bytes\n   * @param encoding - Source encoding name\n   * @returns Decoded UTF-8 string\n   */\n  decode(buffer: Uint8Array | ArrayBuffer, encoding: string): string;\n\n  /**\n   * Encode a UTF-8 string into the given target encoding.\n   * @param str - Input string\n   * @param encoding - Target encoding name\n   * @returns `string` when `encoding` is `\"UTF8\"` or `\"UTF-8\"`, otherwise `Uint8Array`.\n   */\n  encode<T extends string>(\n    str: string,\n    encoding: T,\n  ): T extends \"UTF8\" | \"UTF-8\" ? string : Uint8Array;\n\n  /**\n   * Returns the list of supported encoding names.\n   * @returns Array of encoding names supported by the platform iconv runtime\n   */\n  encodings(): string[];\n\n  /** Constructor for the stateful converter. */\n  Iconv: IconvConstructor;\n}\n\nconst api = (): TailorIconvAPI =>\n  (globalThis as unknown as { tailor: { iconv: TailorIconvAPI } }).tailor.iconv;\n\nconst convert: TailorIconvAPI[\"convert\"] = (...args) => api().convert(...args);\n\nconst convertBuffer: TailorIconvAPI[\"convertBuffer\"] = (...args) => api().convertBuffer(...args);\n\nconst decode: TailorIconvAPI[\"decode\"] = (...args) => api().decode(...args);\n\nconst encode: TailorIconvAPI[\"encode\"] = (...args) => api().encode(...args);\n\nconst encodings: TailorIconvAPI[\"encodings\"] = () => api().encodings();\n\n/**\n * Stateful converter for repeated conversions between a fixed encoding pair.\n * Compatible with the `node-iconv` API surface.\n */\nclass Iconv {\n  private impl: IconvInstance;\n\n  constructor(fromEncoding: string, toEncoding: string) {\n    this.impl = new (api().Iconv)(fromEncoding, toEncoding);\n  }\n\n  /**\n   * Convert input using this converter's fixed encoding pair.\n   * @param input - Bytes or string to convert\n   * @returns Encoded output (string for UTF-8 targets, otherwise `Uint8Array`).\n   */\n  convert(input: string | Uint8Array | ArrayBuffer): string | Uint8Array {\n    return this.impl.convert(input);\n  }\n}\n\n// Keep the object typed to the public API so the private wrapper class does not leak into d.ts.\n/** Runtime API for `tailor.iconv`. */\nexport const iconv: TailorIconvAPI = {\n  convert,\n  convertBuffer,\n  decode,\n  encode,\n  encodings,\n  Iconv,\n};\n"],"mappings":"AA0FA,MAAM,QACH,WAAgE,OAAO,MAmC7D,EAAwB,CACnC,SAlC0C,GAAG,IAAS,IAAI,CAAC,CAAC,QAAQ,GAAG,CAAI,EAmC3E,eAjCsD,GAAG,IAAS,IAAI,CAAC,CAAC,cAAc,GAAG,CAAI,EAkC7F,QAhCwC,GAAG,IAAS,IAAI,CAAC,CAAC,OAAO,GAAG,CAAI,EAiCxE,QA/BwC,GAAG,IAAS,IAAI,CAAC,CAAC,OAAO,GAAG,CAAI,EAgCxE,cA9BmD,IAAI,CAAC,CAAC,UAAU,EA+BnE,WAzBU,CACV,KAEA,YAAY,EAAsB,EAAoB,CACpD,KAAK,KAAO,IAAK,IAAI,EAAA,CAAE,MAAO,EAAc,CAAU,CACxD,CAOA,QAAQ,EAA+D,CACrE,OAAO,KAAK,KAAK,QAAQ,CAAK,CAChC,CACF,CAWA"}