/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-nocheck
import { resolveUnifiedColorSymbol, getSpritePalette } from "../editorCore";
import { getSpriteRows } from "../graphics/rowEncoding";

export function parseHexColor(value, fallback = 0x060A14) {
  const raw = String(value || "").trim();
  if (!/^#[0-9a-fA-F]{6}$/.test(raw)) {
    return fallback;
  }
  return Number.parseInt(raw.slice(1), 16);
}

export function getWorldBorderThickness(tileSize) {
  return Math.max(0.04, 1 / Math.max(1, Number(tileSize) || 1));
}

export function applyPixelArtTexture(texture) {
  if (!texture?.source) {
    return texture;
  }
  texture.source.scaleMode = "nearest";
  return texture;
}

export function refreshCanvasTexture(texture) {
  try {
    texture?.source?.update?.();
    texture?.update?.();
  } catch {
    // Ignore stale canvas texture refresh issues and let the next rebuild recover.
  }
}

export function createSolidCanvas(width, height, color) {
  const canvas = document.createElement("canvas");
  canvas.width = Math.max(1, Math.floor(Number(width) || 1));
  canvas.height = Math.max(1, Math.floor(Number(height) || 1));
  const ctx = canvas.getContext("2d");
  if (ctx) {
    ctx.imageSmoothingEnabled = false;
    ctx.fillStyle = String(color || "#000000");
    ctx.fillRect(0, 0, canvas.width, canvas.height);
  }
  return canvas;
}

export function buildTileCanvasSurface(tileEntry, fallbackColor) {
  const sourceRows = getSpriteRows(tileEntry);
  const rows = Array.isArray(sourceRows) ? sourceRows.map((row) => String(row || "")) : [];
  const width = Math.max(1, Number(tileEntry?.width) || rows.reduce((max, row) => Math.max(max, row.length), 0) || 1);
  const height = Math.max(1, Number(tileEntry?.height) || rows.length || 1);
  const pixelScale = Math.max(1, Number(tileEntry?.pixelScale) || 1);
  const canvas = document.createElement("canvas");
  canvas.width = width * pixelScale;
  canvas.height = height * pixelScale;
  const ctx = canvas.getContext("2d");
  if (!ctx) {
    return null;
  }
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  for (let y = 0; y < height; y += 1) {
    const row = rows[y] || "";
    for (let x = 0; x < width; x += 1) {
      const symbol = String(row.charAt(x) || ".").charAt(0) || ".";
      if (symbol === ".") {
        continue;
      }
      ctx.fillStyle = resolveUnifiedColorSymbol(symbol, String(fallbackColor || "#00000000"));
      ctx.fillRect(x * pixelScale, y * pixelScale, pixelScale, pixelScale);
    }
  }
  return canvas;
}

export function buildSpriteCanvasSurface(spriteEntry) {
  const sourceRows = getSpriteRows(spriteEntry);
  const rows = Array.isArray(sourceRows) ? sourceRows.map((row) => String(row || "")) : [];
  const width = Math.max(1, Number(spriteEntry?.width) || rows.reduce((max, row) => Math.max(max, row.length), 0) || 1);
  const height = Math.max(1, Number(spriteEntry?.height) || rows.length || 1);
  const pixelScale = Math.max(1, Number(spriteEntry?.pixelScale) || 1);
  const palette = getSpritePalette(spriteEntry);
  const canvas = document.createElement("canvas");
  canvas.width = width * pixelScale;
  canvas.height = height * pixelScale;
  const ctx = canvas.getContext("2d");
  if (!ctx) {
    return null;
  }
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  for (let y = 0; y < height; y += 1) {
    const row = rows[y] || "";
    for (let x = 0; x < width; x += 1) {
      const symbol = String(row.charAt(x) || ".").charAt(0) || ".";
      if (symbol === ".") {
        continue;
      }
      ctx.fillStyle = String(palette[symbol] || "#00000000");
      ctx.fillRect(x * pixelScale, y * pixelScale, pixelScale, pixelScale);
    }
  }
  return canvas;
}

export function destroyCachedTexture(cacheMap, key) {
  const cachedTexture = cacheMap.get(key) || null;
  if (!cachedTexture) {
    return;
  }
  cacheMap.delete(key);
  try {
    cachedTexture.destroy(true);
  } catch {
    // Ignore stale texture cleanup issues and let Pixi recreate on demand.
  }
}
