import { createFromString, mergePath as mergePath$1, extendFromString } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/tool/path.mjs";
import { identity, mul, invert } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/core/matrix.mjs";
import { applyTransform as applyTransform$1 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/core/vector.mjs";
import Path from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/Path.mjs";
import Transformable from "../../../../../zrender@5.4.4/node_modules/zrender/lib/core/Transformable.mjs";
import ZRImage from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/Image.mjs";
import { default as default2 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/Group.mjs";
import { default as default3 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/Text.mjs";
import Circle from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Circle.mjs";
import Ellipse from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Ellipse.mjs";
import Sector from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Sector.mjs";
import Ring from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Ring.mjs";
import Polygon from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Polygon.mjs";
import Polyline from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Polyline.mjs";
import Rect from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Rect.mjs";
import Line from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Line.mjs";
import BezierCurve from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/BezierCurve.mjs";
import Arc from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Arc.mjs";
import { default as default4 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/CompoundPath.mjs";
import { default as default5 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/LinearGradient.mjs";
import { default as default6 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/RadialGradient.mjs";
import { default as default7 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/core/BoundingRect.mjs";
import { default as default8 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/core/OrientedBoundingRect.mjs";
import { default as default9 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/core/Point.mjs";
import { default as default10 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/IncrementalDisplayable.mjs";
import { subPixelOptimizeLine as subPixelOptimizeLine$1, subPixelOptimize as subPixelOptimize$1, subPixelOptimizeRect as subPixelOptimizeRect$1 } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/helper/subPixelOptimize.mjs";
import { isArray, isArrayLike, extend, defaults, each, hasOwn, keys, map, isString } from "../../../../../zrender@5.4.4/node_modules/zrender/lib/core/util.mjs";
import { getECData } from "./innerStore.mjs";
import { updateProps } from "../animation/basicTransition.mjs";
import { initProps, isElementRemoved, removeElement, removeElementWithFadeOut } from "../animation/basicTransition.mjs";
var mathMax = Math.max;
var mathMin = Math.min;
var _customShapeMap = {};
function extendShape(opts) {
  return Path.extend(opts);
}
var extendPathFromString = extendFromString;
function extendPath(pathData, opts) {
  return extendPathFromString(pathData, opts);
}
function registerShape(name, ShapeClass) {
  _customShapeMap[name] = ShapeClass;
}
function getShapeClass(name) {
  if (_customShapeMap.hasOwnProperty(name)) {
    return _customShapeMap[name];
  }
}
function makePath(pathData, opts, rect, layout) {
  var path = createFromString(pathData, opts);
  if (rect) {
    if (layout === "center") {
      rect = centerGraphic(rect, path.getBoundingRect());
    }
    resizePath(path, rect);
  }
  return path;
}
function makeImage(imageUrl, rect, layout) {
  var zrImg = new ZRImage({
    style: {
      image: imageUrl,
      x: rect.x,
      y: rect.y,
      width: rect.width,
      height: rect.height
    },
    onload: function(img) {
      if (layout === "center") {
        var boundingRect = {
          width: img.width,
          height: img.height
        };
        zrImg.setStyle(centerGraphic(rect, boundingRect));
      }
    }
  });
  return zrImg;
}
function centerGraphic(rect, boundingRect) {
  var aspect = boundingRect.width / boundingRect.height;
  var width = rect.height * aspect;
  var height;
  if (width <= rect.width) {
    height = rect.height;
  } else {
    width = rect.width;
    height = width / aspect;
  }
  var cx = rect.x + rect.width / 2;
  var cy = rect.y + rect.height / 2;
  return {
    x: cx - width / 2,
    y: cy - height / 2,
    width,
    height
  };
}
var mergePath = mergePath$1;
function resizePath(path, rect) {
  if (!path.applyTransform) {
    return;
  }
  var pathRect = path.getBoundingRect();
  var m = pathRect.calculateTransform(rect);
  path.applyTransform(m);
}
function subPixelOptimizeLine(shape, lineWidth) {
  subPixelOptimizeLine$1(shape, shape, {
    lineWidth
  });
  return shape;
}
function subPixelOptimizeRect(param) {
  subPixelOptimizeRect$1(param.shape, param.shape, param.style);
  return param;
}
var subPixelOptimize = subPixelOptimize$1;
function getTransform(target, ancestor) {
  var mat = identity([]);
  while (target && target !== ancestor) {
    mul(mat, target.getLocalTransform(), mat);
    target = target.parent;
  }
  return mat;
}
function applyTransform(target, transform, invert$1) {
  if (transform && !isArrayLike(transform)) {
    transform = Transformable.getLocalTransform(transform);
  }
  if (invert$1) {
    transform = invert([], transform);
  }
  return applyTransform$1([], target, transform);
}
function transformDirection(direction, transform, invert2) {
  var hBase = transform[4] === 0 || transform[5] === 0 || transform[0] === 0 ? 1 : Math.abs(2 * transform[4] / transform[0]);
  var vBase = transform[4] === 0 || transform[5] === 0 || transform[2] === 0 ? 1 : Math.abs(2 * transform[4] / transform[2]);
  var vertex = [direction === "left" ? -hBase : direction === "right" ? hBase : 0, direction === "top" ? -vBase : direction === "bottom" ? vBase : 0];
  vertex = applyTransform(vertex, transform, invert2);
  return Math.abs(vertex[0]) > Math.abs(vertex[1]) ? vertex[0] > 0 ? "right" : "left" : vertex[1] > 0 ? "bottom" : "top";
}
function isNotGroup(el) {
  return !el.isGroup;
}
function isPath(el) {
  return el.shape != null;
}
function groupTransition(g1, g2, animatableModel) {
  if (!g1 || !g2) {
    return;
  }
  function getElMap(g) {
    var elMap = {};
    g.traverse(function(el) {
      if (isNotGroup(el) && el.anid) {
        elMap[el.anid] = el;
      }
    });
    return elMap;
  }
  function getAnimatableProps(el) {
    var obj = {
      x: el.x,
      y: el.y,
      rotation: el.rotation
    };
    if (isPath(el)) {
      obj.shape = extend({}, el.shape);
    }
    return obj;
  }
  var elMap1 = getElMap(g1);
  g2.traverse(function(el) {
    if (isNotGroup(el) && el.anid) {
      var oldEl = elMap1[el.anid];
      if (oldEl) {
        var newProp = getAnimatableProps(el);
        el.attr(getAnimatableProps(oldEl));
        updateProps(el, newProp, animatableModel, getECData(el).dataIndex);
      }
    }
  });
}
function clipPointsByRect(points, rect) {
  return map(points, function(point) {
    var x = point[0];
    x = mathMax(x, rect.x);
    x = mathMin(x, rect.x + rect.width);
    var y = point[1];
    y = mathMax(y, rect.y);
    y = mathMin(y, rect.y + rect.height);
    return [x, y];
  });
}
function clipRectByRect(targetRect, rect) {
  var x = mathMax(targetRect.x, rect.x);
  var x2 = mathMin(targetRect.x + targetRect.width, rect.x + rect.width);
  var y = mathMax(targetRect.y, rect.y);
  var y2 = mathMin(targetRect.y + targetRect.height, rect.y + rect.height);
  if (x2 >= x && y2 >= y) {
    return {
      x,
      y,
      width: x2 - x,
      height: y2 - y
    };
  }
}
function createIcon(iconStr, opt, rect) {
  var innerOpts = extend({
    rectHover: true
  }, opt);
  var style = innerOpts.style = {
    strokeNoScale: true
  };
  rect = rect || {
    x: -1,
    y: -1,
    width: 2,
    height: 2
  };
  if (iconStr) {
    return iconStr.indexOf("image://") === 0 ? (style.image = iconStr.slice(8), defaults(style, rect), new ZRImage(innerOpts)) : makePath(iconStr.replace("path://", ""), innerOpts, rect, "center");
  }
}
function linePolygonIntersect(a1x, a1y, a2x, a2y, points) {
  for (var i = 0, p2 = points[points.length - 1]; i < points.length; i++) {
    var p = points[i];
    if (lineLineIntersect(a1x, a1y, a2x, a2y, p[0], p[1], p2[0], p2[1])) {
      return true;
    }
    p2 = p;
  }
}
function lineLineIntersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y) {
  var mx = a2x - a1x;
  var my = a2y - a1y;
  var nx = b2x - b1x;
  var ny = b2y - b1y;
  var nmCrossProduct = crossProduct2d(nx, ny, mx, my);
  if (nearZero(nmCrossProduct)) {
    return false;
  }
  var b1a1x = a1x - b1x;
  var b1a1y = a1y - b1y;
  var q = crossProduct2d(b1a1x, b1a1y, mx, my) / nmCrossProduct;
  if (q < 0 || q > 1) {
    return false;
  }
  var p = crossProduct2d(b1a1x, b1a1y, nx, ny) / nmCrossProduct;
  if (p < 0 || p > 1) {
    return false;
  }
  return true;
}
function crossProduct2d(x1, y1, x2, y2) {
  return x1 * y2 - x2 * y1;
}
function nearZero(val) {
  return val <= 1e-6 && val >= -1e-6;
}
function setTooltipConfig(opt) {
  var itemTooltipOption = opt.itemTooltipOption;
  var componentModel = opt.componentModel;
  var itemName = opt.itemName;
  var itemTooltipOptionObj = isString(itemTooltipOption) ? {
    formatter: itemTooltipOption
  } : itemTooltipOption;
  var mainType = componentModel.mainType;
  var componentIndex = componentModel.componentIndex;
  var formatterParams = {
    componentType: mainType,
    name: itemName,
    $vars: ["name"]
  };
  formatterParams[mainType + "Index"] = componentIndex;
  var formatterParamsExtra = opt.formatterParamsExtra;
  if (formatterParamsExtra) {
    each(keys(formatterParamsExtra), function(key) {
      if (!hasOwn(formatterParams, key)) {
        formatterParams[key] = formatterParamsExtra[key];
        formatterParams.$vars.push(key);
      }
    });
  }
  var ecData = getECData(opt.el);
  ecData.componentMainType = mainType;
  ecData.componentIndex = componentIndex;
  ecData.tooltipConfig = {
    name: itemName,
    option: defaults({
      content: itemName,
      formatterParams
    }, itemTooltipOptionObj)
  };
}
function traverseElement(el, cb) {
  var stopped;
  if (el.isGroup) {
    stopped = cb(el);
  }
  if (!stopped) {
    el.traverse(cb);
  }
}
function traverseElements(els, cb) {
  if (els) {
    if (isArray(els)) {
      for (var i = 0; i < els.length; i++) {
        traverseElement(els[i], cb);
      }
    } else {
      traverseElement(els, cb);
    }
  }
}
registerShape("circle", Circle);
registerShape("ellipse", Ellipse);
registerShape("sector", Sector);
registerShape("ring", Ring);
registerShape("polygon", Polygon);
registerShape("polyline", Polyline);
registerShape("rect", Rect);
registerShape("line", Line);
registerShape("bezierCurve", BezierCurve);
registerShape("arc", Arc);
export {
  Arc,
  BezierCurve,
  default7 as BoundingRect,
  Circle,
  default4 as CompoundPath,
  Ellipse,
  default2 as Group,
  ZRImage as Image,
  default10 as IncrementalDisplayable,
  Line,
  default5 as LinearGradient,
  default8 as OrientedBoundingRect,
  Path,
  default9 as Point,
  Polygon,
  Polyline,
  default6 as RadialGradient,
  Rect,
  Ring,
  Sector,
  default3 as Text,
  applyTransform,
  clipPointsByRect,
  clipRectByRect,
  createIcon,
  extendPath,
  extendShape,
  getShapeClass,
  getTransform,
  groupTransition,
  initProps,
  isElementRemoved,
  lineLineIntersect,
  linePolygonIntersect,
  makeImage,
  makePath,
  mergePath,
  registerShape,
  removeElement,
  removeElementWithFadeOut,
  resizePath,
  setTooltipConfig,
  subPixelOptimize,
  subPixelOptimizeLine,
  subPixelOptimizeRect,
  transformDirection,
  traverseElements,
  updateProps
};
