import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import { Pos, countColumn } from 'codemirror';
import { separate } from '../File/';

var Snippet =
/*#__PURE__*/
function () {
  function Snippet(props) {
    _classCallCheck(this, Snippet);

    this.props = Object.freeze(props);
    this._separate = separate(props.name);
  }

  _createClass(Snippet, [{
    key: "render",
    value: function render(element) {
      element.textContent = this.props.prefix + ' ' + this.props.description;
      return element;
    }
  }, {
    key: "hint",
    value: function hint(cm, self, data) {
      var from = self.asset ? new Pos(self.from.line + 1, 0) : self.from;
      var to = self.asset ? from : self.to;
      var text = self.asset ? data.text + '\n' : data.text;
      var prefix = cm.getLine(from.line);
      cm.replaceRange(text, from, to, 'complete'); // 挿入位置のタブに合わせるインデント

      var indent = countColumn(prefix, null, 4);
      var end = from.line + text.split('\n').length + (self.asset ? -1 : 0);

      for (var line = from.line + 1; line < end; line++) {
        var pos = new Pos(line, 0);
        cm.replaceRange('\t'.repeat(indent / 4), pos, pos, '+input');
      }

      var endLine = from.line + length - 1;
      var endCh = cm.getLine(endLine).length;
      return {
        from: from,
        to: new Pos(endLine, endCh)
      };
    }
  }, {
    key: "text",
    get: function get() {
      if (!this.props.body && typeof this.props.text === 'string') {
        return this.props.text;
      } else if (Array.isArray(this.props.body)) {
        return this.props.body.map(function (text) {
          return text.replace(/\\t/g, '\t');
        }).join('\n');
      } else {
        return this.props.body.replace(/\\n/g, '\n').replace(/\\t/g, '\t');
      }
    }
  }, {
    key: "prefix",
    get: function get() {
      return this.props.prefix || '';
    }
  }, {
    key: "description",
    get: function get() {
      return this.props.description || '';
    }
  }, {
    key: "descriptionMoreURL",
    get: function get() {
      return this.props.descriptionMoreURL;
    }
  }, {
    key: "leftLabel",
    get: function get() {
      return this.props.leftLabelHTML || this.props.leftLabel || '';
    }
  }, {
    key: "rightLabel",
    get: function get() {
      return this.props.rightLabelHTML || this.props.rightLabel || '';
    }
  }, {
    key: "plain",
    get: function get() {
      return this._separate.plain;
    }
  }, {
    key: "fileKey",
    get: function get() {
      throw new Error('Snippet.fileKey is not supported');
    }
  }]);

  return Snippet;
}();

export { Snippet as default };