"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

require("regenerator-runtime/runtime.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.promise.js");

require("core-js/modules/es.array.iterator.js");

require("core-js/modules/es.string.iterator.js");

require("core-js/modules/web.dom-collections.iterator.js");

require("core-js/modules/es.array.map.js");

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getSelectionRanges = getSelectionRanges;

var languageModes_1 = require("./languageModes");

var positions_1 = require("../utils/positions");

function getSelectionRanges(_x, _x2, _x3) {
  return _getSelectionRanges.apply(this, arguments);
}

function _getSelectionRanges() {
  _getSelectionRanges = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(languageModes, document, positions) {
    var htmlMode;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            htmlMode = languageModes.getMode('html');
            return _context2.abrupt("return", Promise.all(positions.map( /*#__PURE__*/function () {
              var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(position) {
                var htmlRange, mode, range, top;
                return regeneratorRuntime.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        _context.next = 2;
                        return htmlMode.getSelectionRange(document, position);

                      case 2:
                        htmlRange = _context.sent;
                        mode = languageModes.getModeAtPosition(document, position);

                        if (!(mode && mode.getSelectionRange)) {
                          _context.next = 12;
                          break;
                        }

                        _context.next = 7;
                        return mode.getSelectionRange(document, position);

                      case 7:
                        range = _context.sent;
                        top = range;

                        while (top.parent && (0, positions_1.insideRangeButNotSame)(htmlRange.range, top.parent.range)) {
                          top = top.parent;
                        }

                        top.parent = htmlRange;
                        return _context.abrupt("return", range);

                      case 12:
                        return _context.abrupt("return", htmlRange || languageModes_1.SelectionRange.create(languageModes_1.Range.create(position, position)));

                      case 13:
                      case "end":
                        return _context.stop();
                    }
                  }
                }, _callee);
              }));

              return function (_x4) {
                return _ref.apply(this, arguments);
              };
            }())));

          case 2:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _getSelectionRanges.apply(this, arguments);
}