"use strict";

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

var _react = _interopRequireWildcard(require("react"));

var _reactRedux = require("react-redux");

var _runIt = require("@looker/run-it");

var _DocCode = require("../DocCode");

var _state = require("../../state");

var _path = require("../../utils/path");

var _utils = require("./utils");

var _DocDeclarations = require("./DocDeclarations");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var getDeclarations = (generators, sdkLanguage, item) => {
  var declarations = {};
  Object.entries(generators).forEach(_ref => {
    var [language, gen] = _ref;

    if (sdkLanguage === 'All' || language === sdkLanguage) {
      var code = (0, _path.isMethod)(item) ? gen.declareMethod('', item) : gen.declareType('', item);
      declarations[language] = code;
    }
  });
  return declarations;
};

var DocSDKs = _ref2 => {
  var {
    api,
    method,
    type
  } = _ref2;
  var sdkLanguage = (0, _reactRedux.useSelector)(_state.selectSdkLanguage);
  var generators = (0, _runIt.getGenerators)(api);
  var [item, setItem] = (0, _react.useState)(method ? (0, _utils.noComment)(method) : type);
  var [declarations, setDeclarations] = (0, _react.useState)(getDeclarations(generators, sdkLanguage, item));
  var [header, setHeader] = (0, _react.useState)("".concat(sdkLanguage, " Declaration"));
  (0, _react.useEffect)(() => {
    setItem(method ? (0, _utils.noComment)(method) : type);
  }, [method, type]);
  (0, _react.useEffect)(() => {
    var declarations = getDeclarations(generators, sdkLanguage, item);
    setDeclarations(declarations);
    var languages = Object.keys(declarations);

    if (languages.length > 1) {
      setHeader('Declarations');
    } else {
      setHeader("".concat(languages[0], " Declaration"));
    }
  }, [sdkLanguage, item]);
  return _react.default.createElement(_runIt.CollapserCard, {
    heading: header,
    id: "sdk declarations"
  }, Object.keys(declarations).length > 1 ? _react.default.createElement(_DocDeclarations.DocDeclarations, {
    declarations: declarations
  }) : _react.default.createElement(_DocCode.DocCode, {
    language: Object.keys(declarations)[0],
    code: Object.values(declarations)[0]
  }));
};

exports.DocSDKs = DocSDKs;
//# sourceMappingURL=DocSDKs.js.map