import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { findChildren } from '@atlaskit/editor-prosemirror/utils';
import { roundToNearest } from '../media-single';
import { MEDIA_DYNAMIC_GUIDELINE_PREFIX } from './constants';
import { getMediaSingleDimensions } from './getMediaSingleDimensions';
export var generateDynamicGuidelines = function generateDynamicGuidelines(state, editorWidth) {
  var styles = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var selectedNode = state.selection instanceof NodeSelection && state.selection.node;
  var offset = editorWidth / 2;
  return findChildren(state.tr.doc, function (node) {
    return node.type === state.schema.nodes.mediaSingle;
  }).reduce(function (acc, nodeWithPos, index) {
    var _acc$relativeGuides, _acc$relativeGuides2;
    var node = nodeWithPos.node,
      pos = nodeWithPos.pos;

    // if the current node the selected node
    // or the node is not using pixel width,
    // We will skip the node.
    if (selectedNode === node || node.attrs.widthType !== 'pixel') {
      return acc;
    }
    var $pos = state.tr.doc.resolve(pos);
    if ($pos.parent.type !== state.schema.nodes.doc) {
      return acc;
    }
    var dimensions = getMediaSingleDimensions(node, editorWidth);
    if (!dimensions) {
      return acc;
    }
    var width = dimensions.width,
      height = dimensions.height;
    var dynamicGuides = [].concat(_toConsumableArray(acc.dynamicGuides), _toConsumableArray(getDynamicGuides(node.attrs.layout, width, offset, "".concat(MEDIA_DYNAMIC_GUIDELINE_PREFIX).concat(index), styles)));
    var accRelativeGuidesWidth = ((_acc$relativeGuides = acc.relativeGuides) === null || _acc$relativeGuides === void 0 ? void 0 : _acc$relativeGuides.width) || {};
    var accRelativeGuidesHeight = ((_acc$relativeGuides2 = acc.relativeGuides) === null || _acc$relativeGuides2 === void 0 ? void 0 : _acc$relativeGuides2.height) || {};
    var relativeGuidesWidth = _objectSpread(_objectSpread({}, accRelativeGuidesWidth), {}, _defineProperty({}, width, [].concat(_toConsumableArray(accRelativeGuidesWidth[width] || []), [nodeWithPos])));
    var relativeGuidesWidthHeight = _objectSpread(_objectSpread({}, accRelativeGuidesHeight), {}, _defineProperty({}, Math.round(height), [].concat(_toConsumableArray(accRelativeGuidesHeight[height] || []), [nodeWithPos])));
    return {
      dynamicGuides: dynamicGuides,
      relativeGuides: {
        width: relativeGuidesWidth,
        height: relativeGuidesWidthHeight
      }
    };
  }, {
    relativeGuides: {
      width: {},
      height: {}
    },
    dynamicGuides: []
  });
};
var getDynamicGuides = function getDynamicGuides(layout, width, offset, key, styles) {
  switch (layout) {
    case 'align-start':
    case 'wrap-left':
      return [_objectSpread({
        position: {
          x: roundToNearest(width - offset)
        },
        key: key
      }, styles)];
    case 'align-end':
    case 'wrap-right':
      return [_objectSpread({
        position: {
          x: roundToNearest(offset - width)
        },
        key: key
      }, styles)];
    case 'center':
      return [_objectSpread({
        position: {
          x: roundToNearest(width / 2)
        },
        key: "".concat(key, "_right")
      }, styles), _objectSpread({
        position: {
          x: -roundToNearest(width / 2)
        },
        key: "".concat(key, "_left")
      }, styles)];
    default:
      return [];
  }
};