"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.array.map.js");

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

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

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

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

require("core-js/modules/web.dom-collections.iterator.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.fetchHTMLDataProviders = fetchHTMLDataProviders;

var vscode_html_languageservice_1 = require("vscode-html-languageservice");

function fetchHTMLDataProviders(dataPaths, requestService) {
  var providers = dataPaths.map( /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(p) {
      var content;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _context.next = 3;
              return requestService.getContent(p);

            case 3:
              content = _context.sent;
              return _context.abrupt("return", parseHTMLData(p, content));

            case 7:
              _context.prev = 7;
              _context.t0 = _context["catch"](0);
              return _context.abrupt("return", (0, vscode_html_languageservice_1.newHTMLDataProvider)(p, {
                version: 1
              }));

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[0, 7]]);
    }));

    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }());
  return Promise.all(providers);
}

function parseHTMLData(id, source) {
  var rawData;

  try {
    rawData = JSON.parse(source);
  } catch (err) {
    return (0, vscode_html_languageservice_1.newHTMLDataProvider)(id, {
      version: 1
    });
  }

  return (0, vscode_html_languageservice_1.newHTMLDataProvider)(id, {
    version: rawData.version || 1,
    tags: rawData.tags || [],
    globalAttributes: rawData.globalAttributes || [],
    valueSets: rawData.valueSets || []
  });
}