import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import React from 'react';
import classNames from 'classnames';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { atomOneLight } from 'react-syntax-highlighter/styles/hljs';
import './index.less';
var FONT_SIZE = 12;
var LINE_HEIGHT = 18;
var PADDING_TOP = 5; //TODO: add select with several themes
//TODO: scrool to/highlight crumbed lines
//https://github.com/conorhastings/react-syntax-highlighter/blob/master/README.md

var _default =
/*#__PURE__*/
function (_React$Component) {
  _inherits(_default, _React$Component);

  function _default() {
    _classCallCheck(this, _default);

    return _possibleConstructorReturn(this, _getPrototypeOf(_default).apply(this, arguments));
  }

  _createClass(_default, [{
    key: "fixScroll",
    value: function fixScroll() {
      var _this$props = this.props,
          _this$props$dependenc = _this$props.dependenciesLines,
          dependenciesLines = _this$props$dependenc === void 0 ? [] : _this$props$dependenc,
          _this$props$crumbedLi = _this$props.crumbedLines,
          crumbedLines = _this$props$crumbedLi === void 0 ? [] : _this$props$crumbedLi,
          lineHeight = _this$props.lineHeight;

      if (!this.codeRef || !this.codeRef.scrollTo) {
        return;
      }

      var lines = dependenciesLines.length ? dependenciesLines : crumbedLines.length ? crumbedLines : null;
      lines && this.codeRef.scrollTo(0, (lines[0][0] - 1) * (lineHeight || LINE_HEIGHT) + PADDING_TOP - 2);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      // TODO: don\t do it each time (check real changes)
      this.fixScroll();
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.fixScroll();
    }
  }, {
    key: "render",
    value: function render() {
      var _this = this;

      var _this$props2 = this.props,
          language = _this$props2.language,
          code = _this$props2.code,
          _this$props2$crumbedL = _this$props2.crumbedLines,
          crumbedLines = _this$props2$crumbedL === void 0 ? [] : _this$props2$crumbedL,
          _this$props2$dependen = _this$props2.dependenciesLines,
          dependenciesLines = _this$props2$dependen === void 0 ? [] : _this$props2$dependen,
          limitedHeight = _this$props2.limitedHeight,
          fontSize = _this$props2.fontSize; // TODO: calc height for .Code based on dependenciesLines - it's not always need to be 300 px!!

      return React.createElement("div", {
        className: classNames('Code', {
          limitedHeight: limitedHeight
        }),
        ref: function ref(el) {
          return _this.codeRef = el;
        }
      }, React.createElement(SyntaxHighlighter, {
        language: language,
        style: atomOneLight,
        showLineNumbers: true,
        wrapLines: true,
        customStyle: {
          fontSize: "".concat(fontSize || FONT_SIZE, "px"),
          padding: "".concat(PADDING_TOP, "px 0px 5px 5px")
        },
        lineProps: function lineProps(lineNumber) {
          if (isMatchLineNumber(crumbedLines, lineNumber)) {
            return {
              className: 'crumbedLine'
            };
          }

          if (isMatchLineNumber(dependenciesLines, lineNumber)) {
            return {
              className: 'importedDependencyLine'
            };
          }

          return {};
        }
      }, code || ''));
    }
  }]);

  return _default;
}(React.Component);

export { _default as default };

var isMatchLineNumber = function isMatchLineNumber(lines, lineNumber) {
  return !!lines.find(function (lines) {
    if (lines[0] === lineNumber && lines[1] === lineNumber) {
      return true;
    }

    return lines[0] <= lineNumber && lines[1] >= lineNumber;
  });
};