"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("core-js/modules/es.array.filter.js");

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

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

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

require("regenerator-runtime/runtime.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.getCSSMode = getCSSMode;

var languageModelCache_1 = require("../languageModelCache");

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

var embeddedSupport_1 = require("./embeddedSupport");

function getCSSMode(cssLanguageService, documentRegions, workspace) {
  var embeddedCSSDocuments = (0, languageModelCache_1.getLanguageModelCache)(10, 60, function (document) {
    return documentRegions.get(document).getEmbeddedDocument('css');
  });
  var cssStylesheets = (0, languageModelCache_1.getLanguageModelCache)(10, 60, function (document) {
    return cssLanguageService.parseStylesheet(document);
  });
  return {
    getId: function getId() {
      return 'css';
    },
    doValidation: function doValidation(document) {
      var _arguments = arguments;
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
        var settings, embedded;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                settings = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : workspace.settings;
                embedded = embeddedCSSDocuments.get(document);
                return _context.abrupt("return", cssLanguageService.doValidation(embedded, cssStylesheets.get(embedded), settings && settings.css));

              case 3:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    doComplete: function doComplete(document, position, documentContext) {
      var _arguments2 = arguments;
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
        var _settings$css;

        var _settings, embedded, stylesheet;

        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _settings = _arguments2.length > 3 && _arguments2[3] !== undefined ? _arguments2[3] : workspace.settings;
                embedded = embeddedCSSDocuments.get(document);
                stylesheet = cssStylesheets.get(embedded);
                return _context2.abrupt("return", cssLanguageService.doComplete2(embedded, position, stylesheet, documentContext, _settings === null || _settings === void 0 ? void 0 : (_settings$css = _settings.css) === null || _settings$css === void 0 ? void 0 : _settings$css.completion) || languageModes_1.CompletionList.create());

              case 4:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    },
    doHover: function doHover(document, position) {
      var _arguments3 = arguments;
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
        var _settings$css2;

        var settings, embedded;
        return regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                settings = _arguments3.length > 2 && _arguments3[2] !== undefined ? _arguments3[2] : workspace.settings;
                embedded = embeddedCSSDocuments.get(document);
                return _context3.abrupt("return", cssLanguageService.doHover(embedded, position, cssStylesheets.get(embedded), settings === null || settings === void 0 ? void 0 : (_settings$css2 = settings.css) === null || _settings$css2 === void 0 ? void 0 : _settings$css2.hover));

              case 3:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      }))();
    },
    findDocumentHighlight: function findDocumentHighlight(document, position) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
        var embedded;
        return regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                embedded = embeddedCSSDocuments.get(document);
                return _context4.abrupt("return", cssLanguageService.findDocumentHighlights(embedded, position, cssStylesheets.get(embedded)));

              case 2:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4);
      }))();
    },
    findDocumentSymbols: function findDocumentSymbols(document) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
        var embedded;
        return regeneratorRuntime.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                embedded = embeddedCSSDocuments.get(document);
                return _context5.abrupt("return", cssLanguageService.findDocumentSymbols(embedded, cssStylesheets.get(embedded)).filter(function (s) {
                  return s.name !== embeddedSupport_1.CSS_STYLE_RULE;
                }));

              case 2:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5);
      }))();
    },
    findDefinition: function findDefinition(document, position) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
        var embedded;
        return regeneratorRuntime.wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                embedded = embeddedCSSDocuments.get(document);
                return _context6.abrupt("return", cssLanguageService.findDefinition(embedded, position, cssStylesheets.get(embedded)));

              case 2:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6);
      }))();
    },
    findReferences: function findReferences(document, position) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() {
        var embedded;
        return regeneratorRuntime.wrap(function _callee7$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                embedded = embeddedCSSDocuments.get(document);
                return _context7.abrupt("return", cssLanguageService.findReferences(embedded, position, cssStylesheets.get(embedded)));

              case 2:
              case "end":
                return _context7.stop();
            }
          }
        }, _callee7);
      }))();
    },
    findDocumentColors: function findDocumentColors(document) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8() {
        var embedded;
        return regeneratorRuntime.wrap(function _callee8$(_context8) {
          while (1) {
            switch (_context8.prev = _context8.next) {
              case 0:
                embedded = embeddedCSSDocuments.get(document);
                return _context8.abrupt("return", cssLanguageService.findDocumentColors(embedded, cssStylesheets.get(embedded)));

              case 2:
              case "end":
                return _context8.stop();
            }
          }
        }, _callee8);
      }))();
    },
    getColorPresentations: function getColorPresentations(document, color, range) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() {
        var embedded;
        return regeneratorRuntime.wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                embedded = embeddedCSSDocuments.get(document);
                return _context9.abrupt("return", cssLanguageService.getColorPresentations(embedded, cssStylesheets.get(embedded), color, range));

              case 2:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee9);
      }))();
    },
    getFoldingRanges: function getFoldingRanges(document) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10() {
        var embedded;
        return regeneratorRuntime.wrap(function _callee10$(_context10) {
          while (1) {
            switch (_context10.prev = _context10.next) {
              case 0:
                embedded = embeddedCSSDocuments.get(document);
                return _context10.abrupt("return", cssLanguageService.getFoldingRanges(embedded, {}));

              case 2:
              case "end":
                return _context10.stop();
            }
          }
        }, _callee10);
      }))();
    },
    getSelectionRange: function getSelectionRange(document, position) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee11() {
        var embedded;
        return regeneratorRuntime.wrap(function _callee11$(_context11) {
          while (1) {
            switch (_context11.prev = _context11.next) {
              case 0:
                embedded = embeddedCSSDocuments.get(document);
                return _context11.abrupt("return", cssLanguageService.getSelectionRanges(embedded, [position], cssStylesheets.get(embedded))[0]);

              case 2:
              case "end":
                return _context11.stop();
            }
          }
        }, _callee11);
      }))();
    },
    onDocumentRemoved: function onDocumentRemoved(document) {
      embeddedCSSDocuments.onDocumentRemoved(document);
      cssStylesheets.onDocumentRemoved(document);
    },
    dispose: function dispose() {
      embeddedCSSDocuments.dispose();
      cssStylesheets.dispose();
    }
  };
}