"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm = require("../../../min-dash/dist/index.esm.js");
const index_esm$1 = require("../../../tiny-svg/dist/index.esm.js");
const index_esm$2 = require("../../../min-dom/dist/index.esm.js");
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 index_esm.isObject(t) ? index_esm.assign({ 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 = index_esm$1.create("svg"), index_esm$1.attr(t, {
    id: "helper-svg"
  }), index_esm$2.assignStyle(t, {
    visibility: "hidden",
    position: "fixed",
    width: 0,
    height: 0
  }), document.body.appendChild(t)), t;
}
function y(t) {
  this._config = index_esm.assign({}, {
    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 = index_esm.assign({}, this._config.size, i.box), n = index_esm.assign({}, 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), B = t.split(/\u00AD?\r?\n/), l = [], x = h.width - r.left - r.right, s = index_esm$1.create("text");
  index_esm$1.attr(s, { x: 0, y: 0 }), index_esm$1.attr(s, n);
  var D = k();
  for (index_esm$1.append(D, s); B.length; )
    l.push(P(B, x, s));
  e.vertical === "middle" && (r.top = r.bottom = 0);
  var _ = index_esm.reduce(l, function(a, o, f) {
    return a + (p || o.height);
  }, 0) + r.top + r.bottom, m = index_esm.reduce(l, function(a, o, f) {
    return o.width > a ? o.width : a;
  }, 0), u = r.top;
  e.vertical === "middle" && (u += (h.height - _) / 2), u -= (p || l[0].height) / 4;
  var w = index_esm$1.create("text");
  index_esm$1.attr(w, n), index_esm.forEach(l, function(a) {
    var o;
    switch (u += p || a.height, e.horizontal) {
      case "left":
        o = r.left;
        break;
      case "right":
        o = (g ? m : x) - r.right - a.width;
        break;
      default:
        o = Math.max(((g ? m : x) - a.width) / 2 + r.left, 0);
    }
    var f = index_esm$1.create("tspan");
    index_esm$1.attr(f, { x: o, y: u }), f.textContent = a.text, index_esm$1.append(w, f);
  }), index_esm$1.remove(s);
  var E = {
    width: m,
    height: _
  };
  return {
    dimensions: E,
    element: w
  };
};
function G(t) {
  if ("fontSize" in t && "lineHeight" in t)
    return t.lineHeight * parseInt(t.fontSize, 10);
}
exports.default = y;
