"use strict";

exports.__esModule = true;
exports.updateLabelsForInViewFeatures = updateLabelsForInViewFeatures;
var _isElementInViewport = require("../withEditorInteractions/isElementInViewport");
function updateLabelsForInViewFeatures(_temp) {
  var _ref = _temp === void 0 ? {} : _temp,
    _ref$rectElement = _ref.rectElement,
    rectElement = _ref$rectElement === void 0 ? ".veLinearView" : _ref$rectElement;
  var feats = Array.from(document.querySelectorAll(".veLinearView .veRowViewFeature"));
  var parts = Array.from(document.querySelectorAll(".veLinearView .veRowViewPart"));
  var primers = Array.from(document.querySelectorAll(".veLinearView .veRowViewPrimer"));
  var els = [].concat(feats, parts, primers);
  var boundingRect = document.querySelector(rectElement).getBoundingClientRect();
  els.forEach(function (el) {
    var elBounds = el.getBoundingClientRect();
    var isElIn = (0, _isElementInViewport.isElWithinAnotherEl)(elBounds, boundingRect);
    if (isElIn) {
      var label = el.querySelector(".veLabelText");
      if (!label) return;
      var labelBounds = label.getBoundingClientRect();
      var _isElWithinAnotherElW = (0, _isElementInViewport.isElWithinAnotherElWithDiff)(labelBounds, {
          left: Math.max(boundingRect.left, elBounds.left),
          right: Math.min(boundingRect.right, elBounds.right)
        }),
        isLabelIn = _isElWithinAnotherElW[0],
        diff = _isElWithinAnotherElW[1];
      if (!isLabelIn) {
        var l = window.getComputedStyle(label, null),
          t = l.getPropertyValue("transform");

        // If t return other than "none"
        // Split content into several value
        // The fourth one is the translateX value

        if (t !== "none") {
          var v = t.split("(")[1],
            // w = v.split(")")[0],
            x = v.split(",");
          var newX = Number(x[4]) + diff;
          var newY = Number(x[5].replace(")", ""));
          label.setAttribute("transform", "translate(" + newX + "," + newY + ")");
        }
      }
    }
  });
}