"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
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 shimmer_exports = {};
__export(shimmer_exports, {
  DEFAULT_BASE_COLOR: () => DEFAULT_BASE_COLOR,
  WHITE: () => WHITE,
  bidirectionalSweep: () => bidirectionalSweep,
  blendRgb: () => blendRgb,
  blockKernel: () => blockKernel,
  configToSpec: () => configToSpec,
  directionToSweep: () => directionToSweep,
  frameColors: () => frameColors,
  gradient: () => gradient,
  ltrSweep: () => ltrSweep,
  noSweep: () => noSweep,
  randomSweep: () => randomSweep,
  resolvePalette: () => resolvePalette,
  rtlSweep: () => rtlSweep,
  smoothKernel: () => smoothKernel,
  solidColor: () => solidColor
});
module.exports = __toCommonJS(shimmer_exports);
var import_array = require("../primordials/array");
var import_error = require("../primordials/error");
var import_math = require("../primordials/math");
const DEFAULT_BASE_COLOR = [140, 82, 255];
const WHITE = [255, 255, 255];
function bidirectionalSweep(textLength, padding = 2) {
  const cycle = textLength + 2 * padding;
  const fullCycle = cycle * 2;
  return (frame) => {
    const f = (frame % fullCycle + fullCycle) % fullCycle;
    if (f < cycle) {
      return f - padding;
    }
    return textLength + padding - 1 - (f - cycle);
  };
}
function blendRgb(a, b, t) {
  const k = t < 0 ? 0 : t > 1 ? 1 : t;
  return [
    (0, import_math.MathRound)(a[0] + (b[0] - a[0]) * k),
    (0, import_math.MathRound)(a[1] + (b[1] - a[1]) * k),
    (0, import_math.MathRound)(a[2] + (b[2] - a[2]) * k)
  ];
}
function blockKernel(halfWidth = 1) {
  return (d, ctx) => (0, import_math.MathAbs)(d) <= halfWidth ? ctx.highlightColor : ctx.baseColor;
}
function configToSpec(config, textLength) {
  const dir = config.dir ?? "ltr";
  const padding = config.padding ?? 2;
  const speed = config.speed ?? 1 / 3;
  const baseColor = resolvePalette(config.color, DEFAULT_BASE_COLOR);
  const highlightColor = resolvePalette(config.highlight, WHITE);
  const kernel = config.kernel === "block" ? blockKernel(1) : smoothKernel(config.width ?? 2.5);
  const sweep = directionToSweep(dir, textLength, padding);
  return {
    positionAt: (frame) => sweep(frame * speed),
    kernel,
    baseColor,
    highlightColor
  };
}
function directionToSweep(dir, textLength, padding) {
  switch (dir) {
    case "rtl":
      return rtlSweep(textLength, padding);
    case "bi":
      return bidirectionalSweep(textLength, padding);
    case "random":
      return randomSweep(textLength, padding);
    case "none":
      return noSweep();
    default:
      return ltrSweep(textLength, padding);
  }
}
function frameColors(spec, textLength, frame) {
  const wavePos = spec.positionAt(frame);
  const out = [];
  for (let i = 0; i < textLength; i++) {
    const ctx = {
      __proto__: null,
      baseColor: spec.baseColor(i),
      highlightColor: spec.highlightColor(i)
    };
    out.push(spec.kernel(i - wavePos, ctx));
  }
  return out;
}
function gradient(palette) {
  if (palette.length === 0) {
    throw new import_error.ErrorCtor("gradient palette must not be empty");
  }
  return (i) => palette[i % palette.length];
}
function ltrSweep(textLength, padding = 2) {
  const cycle = textLength + 2 * padding;
  return (frame) => (frame % cycle + cycle) % cycle - padding;
}
function noSweep() {
  return () => -Infinity;
}
function randomSweep(textLength, padding = 2, random = Math.random) {
  const cycle = textLength + 2 * padding;
  let dir = random() < 0.5 ? "ltr" : "rtl";
  let lastCycleIndex = 0;
  return (frame) => {
    const f = (frame % (cycle * 2 ** 30) + cycle * 2 ** 30) % (cycle * 2 ** 30);
    const cycleIndex = (0, import_math.MathFloor)(f / cycle);
    if (cycleIndex !== lastCycleIndex) {
      dir = random() < 0.5 ? "ltr" : "rtl";
      lastCycleIndex = cycleIndex;
    }
    const inCycle = f % cycle;
    return dir === "ltr" ? inCycle - padding : textLength + padding - 1 - inCycle;
  };
}
function resolvePalette(source, defaultColor) {
  if (source === void 0) {
    return solidColor(defaultColor);
  }
  if ((0, import_array.ArrayIsArray)(source[0])) {
    return gradient(source);
  }
  return solidColor(source);
}
function rtlSweep(textLength, padding = 2) {
  const cycle = textLength + 2 * padding;
  return (frame) => textLength + padding - 1 - (frame % cycle + cycle) % cycle;
}
function smoothKernel(halfWidth = 2.5, falloff = 2.5) {
  return (d, ctx) => {
    const dist = (0, import_math.MathAbs)(d);
    if (dist >= halfWidth) {
      return ctx.baseColor;
    }
    const t = (1 - dist / halfWidth) ** falloff;
    return blendRgb(ctx.baseColor, ctx.highlightColor, t);
  };
}
function solidColor(color) {
  return () => color;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DEFAULT_BASE_COLOR,
  WHITE,
  bidirectionalSweep,
  blendRgb,
  blockKernel,
  configToSpec,
  directionToSweep,
  frameColors,
  gradient,
  ltrSweep,
  noSweep,
  randomSweep,
  resolvePalette,
  rtlSweep,
  smoothKernel,
  solidColor
});
