"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index_esm = require("../../../min-dash/dist/index.esm.js");
const ModelUtil = require("./ModelUtil.js");
var DEFAULT_LABEL_SIZE = {
  width: 90,
  height: 20
};
var FLOW_LABEL_INDENT = 15;
function isLabelExternal(semantic) {
  return ModelUtil.is(semantic, "bpmn:Event") || ModelUtil.is(semantic, "bpmn:Gateway") || ModelUtil.is(semantic, "bpmn:DataStoreReference") || ModelUtil.is(semantic, "bpmn:DataObjectReference") || ModelUtil.is(semantic, "bpmn:DataInput") || ModelUtil.is(semantic, "bpmn:DataOutput") || ModelUtil.is(semantic, "bpmn:SequenceFlow") || ModelUtil.is(semantic, "bpmn:MessageFlow") || ModelUtil.is(semantic, "bpmn:Group");
}
function getFlowLabelPosition(waypoints) {
  var mid = waypoints.length / 2 - 1;
  var first = waypoints[Math.floor(mid)];
  var second = waypoints[Math.ceil(mid + 0.01)];
  var position = getWaypointsMid(waypoints);
  var angle = Math.atan((second.y - first.y) / (second.x - first.x));
  var x = position.x, y = position.y;
  if (Math.abs(angle) < Math.PI / 2) {
    y -= FLOW_LABEL_INDENT;
  } else {
    x += FLOW_LABEL_INDENT;
  }
  return { x, y };
}
function getWaypointsMid(waypoints) {
  var mid = waypoints.length / 2 - 1;
  var first = waypoints[Math.floor(mid)];
  var second = waypoints[Math.ceil(mid + 0.01)];
  return {
    x: first.x + (second.x - first.x) / 2,
    y: first.y + (second.y - first.y) / 2
  };
}
function getExternalLabelMid(element) {
  if (element.waypoints) {
    return getFlowLabelPosition(element.waypoints);
  } else if (ModelUtil.is(element, "bpmn:Group")) {
    return {
      x: element.x + element.width / 2,
      y: element.y + DEFAULT_LABEL_SIZE.height / 2
    };
  } else {
    return {
      x: element.x + element.width / 2,
      y: element.y + element.height + DEFAULT_LABEL_SIZE.height / 2
    };
  }
}
function getExternalLabelBounds(di, element) {
  var mid, size, bounds, label = di.label;
  if (label && label.bounds) {
    bounds = label.bounds;
    size = {
      width: Math.max(DEFAULT_LABEL_SIZE.width, bounds.width),
      height: bounds.height
    };
    mid = {
      x: bounds.x + bounds.width / 2,
      y: bounds.y + bounds.height / 2
    };
  } else {
    mid = getExternalLabelMid(element);
    size = DEFAULT_LABEL_SIZE;
  }
  return index_esm.assign({
    x: mid.x - size.width / 2,
    y: mid.y - size.height / 2
  }, size);
}
exports.DEFAULT_LABEL_SIZE = DEFAULT_LABEL_SIZE;
exports.FLOW_LABEL_INDENT = FLOW_LABEL_INDENT;
exports.getExternalLabelBounds = getExternalLabelBounds;
exports.getExternalLabelMid = getExternalLabelMid;
exports.getFlowLabelPosition = getFlowLabelPosition;
exports.getWaypointsMid = getWaypointsMid;
exports.isLabelExternal = isLabelExternal;
