import { assign as q, reduce as x, forEach as o, isObject as E } from "../../../min-dash/dist/index.esm.mjs";
import { create as M$1, attr as N$1, append as _, remove as A } from "../../../tiny-svg/dist/index.esm.mjs";
import { assignStyle as B } from "../../../min-dom/dist/index.esm.mjs";
var M = 0, C = {
  width: 150,
  height: 50
};
function I(t) {
  var i = t.split("-");
  return {
    horizontal: i[0] || "center",
    vertical: i[1] || "top"
  };
}
function F(t) {
  return E(t) ? q({ top: 0, left: 0, right: 0, bottom: 0 }, t) : {
    top: t,
    left: t,
    right: t,
    bottom: t
  };
}
function O(t, i) {
  i.textContent = t;
  var h;
  try {
    var n, e = t === "";
    return i.textContent = e ? "dummy" : t, h = i.getBBox(), n = {
      width: h.width + h.x * 2,
      height: h.height
    }, e && (n.width = 0), n;
  } catch {
    return { width: 0, height: 0 };
  }
}
function P(t, i, h) {
  for (var n = t.shift(), e = n, r; ; ) {
    if (r = O(e, h), r.width = e ? r.width : 0, e === " " || e === "" || r.width < Math.round(i) || e.length < 2)
      return j(t, e, n, r);
    e = U(e, r.width, i);
  }
}
function j(t, i, h, n) {
  if (i.length < h.length) {
    var e = h.slice(i.length).trim();
    t.unshift(e);
  }
  return {
    width: n.width,
    height: n.height,
    text: i
  };
}
var b = "­";
function N(t, i) {
  var h = t.split(/(\s|-|\u00AD)/g), n, e = [], r = 0;
  if (h.length > 1)
    for (; n = h.shift(); )
      if (n.length + r < i)
        e.push(n), r += n.length;
      else {
        (n === "-" || n === b) && e.pop();
        break;
      }
  var g = e[e.length - 1];
  return g && g === b && (e[e.length - 1] = "-"), e.join("");
}
function U(t, i, h) {
  var n = Math.max(t.length * (h / i), 1), e = N(t, n);
  return e || (e = t.slice(0, Math.max(Math.round(n - 1), 1))), e;
}
function k() {
  var t = document.getElementById("helper-svg");
  return t || (t = M$1("svg"), N$1(t, {
    id: "helper-svg"
  }), B(t, {
    visibility: "hidden",
    position: "fixed",
    width: 0,
    height: 0
  }), document.body.appendChild(t)), t;
}
function y(t) {
  this._config = q({}, {
    size: C,
    padding: M,
    style: {},
    align: "center-top"
  }, t || {});
}
y.prototype.createText = function(t, i) {
  return this.layoutText(t, i).element;
};
y.prototype.getDimensions = function(t, i) {
  return this.layoutText(t, i).dimensions;
};
y.prototype.layoutText = function(t, i) {
  var h = q({}, this._config.size, i.box), n = q({}, this._config.style, i.style), e = I(i.align || this._config.align), r = F(i.padding !== void 0 ? i.padding : this._config.padding), g = i.fitBox || false, p = G(n), B2 = t.split(/\u00AD?\r?\n/), l = [], x$1 = h.width - r.left - r.right, s = M$1("text");
  N$1(s, { x: 0, y: 0 }), N$1(s, n);
  var D = k();
  for (_(D, s); B2.length; )
    l.push(P(B2, x$1, s));
  e.vertical === "middle" && (r.top = r.bottom = 0);
  var _$1 = x(l, function(a, o2, f) {
    return a + (p || o2.height);
  }, 0) + r.top + r.bottom, m = x(l, function(a, o2, f) {
    return o2.width > a ? o2.width : a;
  }, 0), u = r.top;
  e.vertical === "middle" && (u += (h.height - _$1) / 2), u -= (p || l[0].height) / 4;
  var w = M$1("text");
  N$1(w, n), o(l, function(a) {
    var o2;
    switch (u += p || a.height, e.horizontal) {
      case "left":
        o2 = r.left;
        break;
      case "right":
        o2 = (g ? m : x$1) - r.right - a.width;
        break;
      default:
        o2 = Math.max(((g ? m : x$1) - a.width) / 2 + r.left, 0);
    }
    var f = M$1("tspan");
    N$1(f, { x: o2, y: u }), f.textContent = a.text, _(w, f);
  }), A(s);
  var E2 = {
    width: m,
    height: _$1
  };
  return {
    dimensions: E2,
    element: w
  };
};
function G(t) {
  if ("fontSize" in t && "lineHeight" in t)
    return t.lineHeight * parseInt(t.fontSize, 10);
}
export {
  y as default
};
