"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const util = require("../core/util.js");
const color = require("../tool/color.js");
const env = require("../core/env.js");
var mathRound = Math.round;
function normalizeColor(color$1) {
  var opacity;
  if (!color$1 || color$1 === "transparent") {
    color$1 = "none";
  } else if (typeof color$1 === "string" && color$1.indexOf("rgba") > -1) {
    var arr = color.parse(color$1);
    if (arr) {
      color$1 = "rgb(" + arr[0] + "," + arr[1] + "," + arr[2] + ")";
      opacity = arr[3];
    }
  }
  return {
    color: color$1,
    opacity: opacity == null ? 1 : opacity
  };
}
var EPSILON = 1e-4;
function isAroundZero(transform) {
  return transform < EPSILON && transform > -EPSILON;
}
function round3(transform) {
  return mathRound(transform * 1e3) / 1e3;
}
function round4(transform) {
  return mathRound(transform * 1e4) / 1e4;
}
function getMatrixStr(m) {
  return "matrix(" + round3(m[0]) + "," + round3(m[1]) + "," + round3(m[2]) + "," + round3(m[3]) + "," + round4(m[4]) + "," + round4(m[5]) + ")";
}
var TEXT_ALIGN_TO_ANCHOR = {
  left: "start",
  right: "end",
  center: "middle",
  middle: "middle"
};
function adjustTextY(y, lineHeight, textBaseline) {
  if (textBaseline === "top") {
    y += lineHeight / 2;
  } else if (textBaseline === "bottom") {
    y -= lineHeight / 2;
  }
  return y;
}
function hasShadow(style) {
  return style && (style.shadowBlur || style.shadowOffsetX || style.shadowOffsetY);
}
function getShadowKey(displayable) {
  var style = displayable.style;
  var globalScale = displayable.getGlobalScale();
  return [
    style.shadowColor,
    (style.shadowBlur || 0).toFixed(2),
    (style.shadowOffsetX || 0).toFixed(2),
    (style.shadowOffsetY || 0).toFixed(2),
    globalScale[0],
    globalScale[1]
  ].join(",");
}
function isImagePattern(val) {
  return val && !!val.image;
}
function isSVGPattern(val) {
  return val && !!val.svgElement;
}
function isPattern(val) {
  return isImagePattern(val) || isSVGPattern(val);
}
function isLinearGradient(val) {
  return val.type === "linear";
}
function isRadialGradient(val) {
  return val.type === "radial";
}
function isGradient(val) {
  return val && (val.type === "linear" || val.type === "radial");
}
function getIdURL(id) {
  return "url(#" + id + ")";
}
function getPathPrecision(el) {
  var scale = el.getGlobalScale();
  var size = Math.max(scale[0], scale[1]);
  return Math.max(Math.ceil(Math.log(size) / Math.log(10)), 1);
}
function getSRTTransformString(transform) {
  var x = transform.x || 0;
  var y = transform.y || 0;
  var rotation = (transform.rotation || 0) * util.RADIAN_TO_DEGREE;
  var scaleX = util.retrieve2(transform.scaleX, 1);
  var scaleY = util.retrieve2(transform.scaleY, 1);
  var skewX = transform.skewX || 0;
  var skewY = transform.skewY || 0;
  var res = [];
  if (x || y) {
    res.push("translate(" + x + "px," + y + "px)");
  }
  if (rotation) {
    res.push("rotate(" + rotation + ")");
  }
  if (scaleX !== 1 || scaleY !== 1) {
    res.push("scale(" + scaleX + "," + scaleY + ")");
  }
  if (skewX || skewY) {
    res.push("skew(" + mathRound(skewX * util.RADIAN_TO_DEGREE) + "deg, " + mathRound(skewY * util.RADIAN_TO_DEGREE) + "deg)");
  }
  return res.join(" ");
}
var encodeBase64 = function() {
  if (env.default.hasGlobalWindow && util.isFunction(window.btoa)) {
    return function(str) {
      return window.btoa(unescape(encodeURIComponent(str)));
    };
  }
  if (typeof Buffer !== "undefined") {
    return function(str) {
      return Buffer.from(str).toString("base64");
    };
  }
  return function(str) {
    if (process.env.NODE_ENV !== "production") {
      util.logError("Base64 isn't natively supported in the current environment.");
    }
    return null;
  };
}();
exports.TEXT_ALIGN_TO_ANCHOR = TEXT_ALIGN_TO_ANCHOR;
exports.adjustTextY = adjustTextY;
exports.encodeBase64 = encodeBase64;
exports.getIdURL = getIdURL;
exports.getMatrixStr = getMatrixStr;
exports.getPathPrecision = getPathPrecision;
exports.getSRTTransformString = getSRTTransformString;
exports.getShadowKey = getShadowKey;
exports.hasShadow = hasShadow;
exports.isAroundZero = isAroundZero;
exports.isGradient = isGradient;
exports.isImagePattern = isImagePattern;
exports.isLinearGradient = isLinearGradient;
exports.isPattern = isPattern;
exports.isRadialGradient = isRadialGradient;
exports.isSVGPattern = isSVGPattern;
exports.normalizeColor = normalizeColor;
exports.round3 = round3;
exports.round4 = round4;
