"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const DataEditor = require("@glideapps/glide-data-grid");
const kit_DataGrid_customRenderers_utils = require("../utils.cjs");
const LINK_CELL = "link-cell";
const TAG_HEIGHT = 18;
const TAG_CONTENT = "Unmanaged";
function onClickSelect(e) {
  const useCtrl = e.cell.data.navigateOn !== "click";
  if (useCtrl !== e.ctrlKey)
    return void 0;
  const canvas = document.createElement("canvas");
  const ctx = canvas.getContext("2d", { alpha: false });
  if (ctx === null)
    return;
  const { posX: hoverX, bounds: rect, cell, theme } = e;
  const font = `${theme.baseFontStyle} ${theme.fontFamily}`;
  ctx.font = font;
  const { link } = cell.data;
  const rectHoverX = rect.x + hoverX;
  const isHovered = rectHoverX > rect.x && rectHoverX < rect.x + rect.width;
  if (isHovered) {
    return link;
  }
  return void 0;
}
const renderer = {
  draw: (args, cell) => {
    const { ctx, rect, theme, hoverX = -100, highlighted } = args;
    const { link, underlineOffset = 5 } = cell.data;
    if (link === void 0)
      return;
    const xPad = theme.cellHorizontalPadding;
    let drawX = rect.x + xPad;
    const rectHoverX = rect.x + hoverX;
    const font = `${theme.baseFontStyle} ${theme.fontFamily}`;
    ctx.font = font;
    const middleCenterBias = DataEditor.getMiddleCenterBias(ctx, font);
    const drawY = rect.y + rect.height / 2 + middleCenterBias;
    const metrics = DataEditor.measureTextCached(link.title, ctx, font);
    const commaMetrics = metrics;
    const isHovered = rectHoverX > rect.x && rectHoverX < rect.x + rect.width;
    if (isHovered) {
      ctx.moveTo(drawX, Math.floor(drawY + underlineOffset) + 0.5);
      ctx.lineTo(drawX + metrics.width, Math.floor(drawY + underlineOffset) + 0.5);
      ctx.strokeStyle = theme.linkColor;
      ctx.stroke();
      ctx.fillStyle = highlighted ? DataEditor.blend(theme.accentLight, theme.bgCell) : theme.bgCell;
      ctx.fillText(link.title, drawX - 1, drawY);
      ctx.fillText(link.title, drawX + 1, drawY);
      ctx.fillText(link.title, drawX - 2, drawY);
      ctx.fillText(link.title, drawX + 2, drawY);
    }
    ctx.fillStyle = link.title ? theme.linkColor : theme.textDark;
    ctx.fillText(link.title || "-", drawX, drawY);
    if (link.unmanaged) {
      const x = drawX + commaMetrics.width + 8;
      const y = drawY - TAG_HEIGHT / 2;
      const tagFont = `600 11px ${theme.fontFamily}`;
      ctx.fillStyle = "#132231";
      ctx.lineWidth = 2;
      ctx.strokeStyle = theme.textBubble;
      ctx.beginPath();
      ctx.font = tagFont;
      kit_DataGrid_customRenderers_utils.roundedRect(
        ctx,
        x,
        y,
        DataEditor.measureTextCached(TAG_CONTENT, ctx, tagFont).width + 10,
        TAG_HEIGHT,
        4
      );
      ctx.stroke();
      ctx.fill();
      ctx.fillStyle = "#fff";
      ctx.fillText(TAG_CONTENT, x + 4, y + TAG_HEIGHT / 2 + DataEditor.getMiddleCenterBias(ctx, tagFont));
    }
    ctx.closePath();
    drawX += commaMetrics.width + 4;
    return true;
  },
  isMatch: (c) => c.data.kind === LINK_CELL,
  kind: DataEditor.GridCellKind.Custom,
  measure: (ctx, cell, theme) => {
    const { link } = cell.data;
    if (link === void 0)
      return 0;
    return ctx.measureText(link.title).width + theme.cellHorizontalPadding * 2;
  },
  needsHover: true,
  needsHoverPosition: true,
  onSelect: (e) => {
    if (onClickSelect(e) !== void 0) {
      e.preventDefault();
    }
  },
  provideEditor: () => void 0
};
exports.LINK_CELL = LINK_CELL;
exports.default = renderer;
