"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getSpecKey = exports.buildPath = exports.isMethod = exports.oAuthPath = exports.diffPath = exports.buildTypePath = exports.buildMethodPath = void 0;

var _sdkCodegen = require("@looker/sdk-codegen");

function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(re, groups) { return new BabelRegExp(re, undefined, groups); }; var _super = RegExp.prototype; var _groups = new WeakMap(); function BabelRegExp(re, flags, groups) { var _this = new RegExp(re, flags); _groups.set(_this, groups || _groups.get(re)); return _setPrototypeOf(_this, BabelRegExp.prototype); } _inherits(BabelRegExp, RegExp); BabelRegExp.prototype.exec = function (str) { var result = _super.exec.call(this, str); if (result) result.groups = buildGroups(result, this); return result; }; BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { if (typeof substitution === "string") { var groups = _groups.get(this); return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) { return "$" + groups[name]; })); } else if (typeof substitution === "function") { var _this = this; return _super[Symbol.replace].call(this, str, function () { var args = arguments; if (typeof args[args.length - 1] !== "object") { args = [].slice.call(args); args.push(buildGroups(args, _this)); } return substitution.apply(this, args); }); } else { return _super[Symbol.replace].call(this, str, substitution); } }; function buildGroups(result, re) { var g = _groups.get(re); return Object.keys(g).reduce(function (groups, name) { groups[name] = result[g[name]]; return groups; }, Object.create(null)); } return _wrapRegExp.apply(this, arguments); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var buildMethodPath = (specKey, tag, methodName) => "/".concat(specKey, "/methods/").concat(tag, "/").concat(methodName);

exports.buildMethodPath = buildMethodPath;

var buildTypePath = (specKey, tag, typeName) => "/".concat(specKey, "/types/").concat(tag, "/").concat(typeName);

exports.buildTypePath = buildTypePath;
var diffPath = 'diff';
exports.diffPath = diffPath;
var oAuthPath = 'oauth';
exports.oAuthPath = oAuthPath;

var getMethodTag = (api, methodName) => {
  return Object.entries(api.tags).filter(_ref => {
    var [, methods] = _ref;
    return methodName in methods;
  }).map(_ref2 => {
    var [methodTag] = _ref2;
    return methodTag;
  })[0];
};

var isMethod = item => 'params' in item;

exports.isMethod = isMethod;

var getTypeTag = (api, type) => {
  var method = (0, _sdkCodegen.firstMethodRef)(api, type);
  return getMethodTag(api, method.name);
};

var buildPath = (api, item, specKey) => {
  var path;

  if (isMethod(item)) {
    var tag = getMethodTag(api, item.name);
    path = buildMethodPath(specKey, tag, item.name);
  } else {
    var _tag = getTypeTag(api, item);

    path = buildTypePath(specKey, _tag, item.name);
  }

  return path;
};

exports.buildPath = buildPath;

var getSpecKey = location => {
  var _match, _match$groups;

  var pathname = location.pathname;
  var match;

  if (pathname.startsWith("/".concat(diffPath))) {
    var pattern = new RegExp("(?:/".concat(diffPath, ")/(?<specKey>\\w+.\\w+)"));
    match = pathname.match(pattern);
  } else {
    match = pathname.match(_wrapRegExp(/\/([0-9A-Z_a-z]+\.[0-9A-Z_a-z]+).*/, {
      specKey: 1
    }));
  }

  return ((_match = match) === null || _match === void 0 ? void 0 : (_match$groups = _match.groups) === null || _match$groups === void 0 ? void 0 : _match$groups.specKey) || null;
};

exports.getSpecKey = getSpecKey;
//# sourceMappingURL=path.js.map