"use strict";
/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var text_shimmer_exports = {};
__export(text_shimmer_exports, {
  COLOR_INHERIT: () => COLOR_INHERIT,
  DIR_LTR: () => DIR_LTR,
  DIR_NONE: () => DIR_NONE,
  DIR_RANDOM: () => DIR_RANDOM,
  DIR_RTL: () => DIR_RTL,
  MODE_BI: () => MODE_BI,
  applyShimmer: () => applyShimmer
});
module.exports = __toCommonJS(text_shimmer_exports);
var import_ansi = require("../ansi");
var import_arrays = require("../arrays");
var import_ci = require("#env/ci");
var import_utils = require("../themes/utils");
var import_themes = require("../themes/themes");
function detectStyles(text) {
  return {
    __proto__: null,
    // biome-ignore lint/suspicious/noControlCharactersInRegex: ANSI escape sequence detection.
    bold: /\x1b\[1m/.test(text),
    // biome-ignore lint/suspicious/noControlCharactersInRegex: ANSI escape sequence detection.
    dim: /\x1b\[2m/.test(text),
    // biome-ignore lint/suspicious/noControlCharactersInRegex: ANSI escape sequence detection.
    italic: /\x1b\[3m/.test(text),
    // biome-ignore lint/suspicious/noControlCharactersInRegex: ANSI escape sequence detection.
    strikethrough: /\x1b\[9m/.test(text),
    // biome-ignore lint/suspicious/noControlCharactersInRegex: ANSI escape sequence detection.
    underline: /\x1b\[4m/.test(text)
  };
}
function stylesToAnsi(styles) {
  let codes = "";
  if (styles.bold) {
    codes += "\x1B[1m";
  }
  if (styles.dim) {
    codes += "\x1B[2m";
  }
  if (styles.italic) {
    codes += "\x1B[3m";
  }
  if (styles.underline) {
    codes += "\x1B[4m";
  }
  if (styles.strikethrough) {
    codes += "\x1B[9m";
  }
  return codes;
}
const COLOR_INHERIT = "inherit";
const DIR_LTR = "ltr";
const DIR_NONE = "none";
const DIR_RANDOM = "random";
const DIR_RTL = "rtl";
const MODE_BI = "bi";
function shimmerIntensity(distance, shimmerWidth = 2.5) {
  if (distance > shimmerWidth) {
    return 0;
  }
  const normalized = distance / shimmerWidth;
  return (1 - normalized) ** 2.5;
}
function blendColors(color1, color2, factor) {
  const r = Math.round(color1[0] + (color2[0] - color1[0]) * factor);
  const g = Math.round(color1[1] + (color2[1] - color1[1]) * factor);
  const b = Math.round(color1[2] + (color2[2] - color1[2]) * factor);
  return [r, g, b];
}
function renderChar(char, index, shimmerPos, baseColor, styles) {
  const distance = Math.abs(index - shimmerPos);
  const intensity = shimmerIntensity(distance);
  const styleCode = stylesToAnsi(styles);
  const charColor = (0, import_arrays.isArray)(baseColor[0]) ? baseColor[index % baseColor.length] ?? [
    140,
    82,
    255
  ] : baseColor;
  if (intensity === 0) {
    const base = `\x1B[38;2;${charColor[0]};${charColor[1]};${charColor[2]}m`;
    return `${styleCode}${base}${char}${import_ansi.ANSI_RESET}`;
  }
  const white = [255, 255, 255];
  const blended = blendColors(charColor, white, intensity);
  const color = `\x1B[38;2;${blended[0]};${blended[1]};${blended[2]}m`;
  return `${styleCode}${color}${char}${import_ansi.ANSI_RESET}`;
}
function getShimmerPos(textLength, step, currentDir, shimmerWidth = 2.5) {
  const totalSteps = textLength + shimmerWidth + 2;
  if (currentDir === DIR_RTL) {
    return textLength - step % totalSteps;
  }
  return step % totalSteps;
}
function pickDirection(direction) {
  if (direction === DIR_RANDOM) {
    return Math.random() < 0.5 ? DIR_LTR : DIR_RTL;
  }
  if (direction === DIR_RTL) {
    return DIR_RTL;
  }
  return DIR_LTR;
}
function applyShimmer(text, state, options) {
  const opts = { __proto__: null, ...options };
  const direction = opts.direction ?? DIR_NONE;
  const shimmerWidth = opts.shimmerWidth ?? 2.5;
  let color;
  if (opts.theme) {
    const theme = typeof opts.theme === "string" ? import_themes.THEMES[opts.theme] : opts.theme;
    const themeColor = (0, import_utils.resolveColor)(
      theme.colors.primary,
      theme.colors
    );
    color = typeof themeColor === "string" ? [140, 82, 255] : themeColor;
  } else {
    color = opts.color ?? [140, 82, 255];
  }
  const styles = opts.styles ?? detectStyles(text);
  const plainText = (0, import_ansi.stripAnsi)(text);
  if ((0, import_ci.getCI)() || !plainText || direction === DIR_NONE) {
    const styleCode = stylesToAnsi(styles);
    const isGradient = (0, import_arrays.isArray)(color[0]);
    return plainText.split("").map((char, i) => {
      const charColor = isGradient ? color[i % color.length] ?? [
        140,
        82,
        255
      ] : color;
      const base = `\x1B[38;2;${charColor[0]};${charColor[1]};${charColor[2]}m`;
      return `${styleCode}${base}${char}${import_ansi.ANSI_RESET}`;
    }).join("");
  }
  const shimmerPos = getShimmerPos(
    plainText.length,
    state.step,
    state.currentDir,
    shimmerWidth
  );
  const result = plainText.split("").map((char, i) => renderChar(char, i, shimmerPos, color, styles)).join("");
  state.step += state.speed;
  const totalSteps = plainText.length + shimmerWidth + 2;
  if (state.mode === MODE_BI) {
    if (state.step >= totalSteps) {
      state.step = 0;
      state.currentDir = state.currentDir === DIR_LTR ? DIR_RTL : DIR_LTR;
    }
  } else if (state.mode === DIR_RANDOM) {
    if (state.step >= totalSteps) {
      state.step = 0;
      state.currentDir = pickDirection(DIR_RANDOM);
    }
  } else {
    if (state.step >= totalSteps) {
      state.step = 0;
    }
  }
  return result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  COLOR_INHERIT,
  DIR_LTR,
  DIR_NONE,
  DIR_RANDOM,
  DIR_RTL,
  MODE_BI,
  applyShimmer
});
