"use strict";

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

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

var _components = require("@looker/components");

var _icons = require("@looker/icons");

var _reactRouterDom = require("react-router-dom");

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

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

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

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

var _components2 = require("../../components");

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

var _components3 = require("./components");

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; }

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); }); }; }

var showRunIt = function () {
  var _ref = _asyncToGenerator(function* (adaptor) {
    var data = yield adaptor.localStorageGetItem(_runIt.RunItFormKey);
    return !!data;
  });

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

var MethodScene = _ref2 => {
  var {
    api
  } = _ref2;
  var adaptor = (0, _utils.getApixAdaptor)();
  var history = (0, _reactRouterDom.useHistory)();
  var sdkLanguage = (0, _reactRedux.useSelector)(_state.selectSdkLanguage);
  var {
    specKey,
    methodTag,
    methodName
  } = (0, _reactRouterDom.useParams)();
  var {
    value,
    toggle,
    setOn
  } = (0, _components.useToggle)();
  var [method, setMethod] = (0, _react.useState)(api.methods[methodName]);
  var seeTypes = (0, _sdkCodegen.typeRefs)(api, method === null || method === void 0 ? void 0 : method.customTypes);
  var RunItButton = value ? _components.Button : _components.ButtonOutline;
  (0, _react.useEffect)(() => {
    var foundMethod = api.methods[methodName];

    if (foundMethod) {
      setMethod(api.methods[methodName]);
    } else {
      if (api.tags[methodTag]) {
        history.push("/".concat(specKey, "/methods/").concat(methodTag));
      } else {
        history.push("/".concat(specKey, "/methods"));
      }
    }
  }, [api, history, methodName, methodTag, specKey]);
  (0, _react.useEffect)(() => {
    var checkRunIt = function () {
      var _ref3 = _asyncToGenerator(function* () {
        try {
          var show = yield showRunIt(adaptor);

          if (show) {
            setOn();
          }
        } catch (error) {
          console.error(error);
        }
      });

      return function checkRunIt() {
        return _ref3.apply(this, arguments);
      };
    }();

    checkRunIt();
  }, [adaptor, setOn]);

  var runItToggle = _react.default.createElement(RunItButton, {
    color: value ? 'key' : 'neutral',
    iconBefore: _react.default.createElement(_icons.Beaker, null),
    onClick: toggle
  }, "Run It");

  return _react.default.createElement(_react.default.Fragment, null, method && _react.default.createElement(_components2.ApixSection, null, _react.default.createElement(_components.Space, {
    between: true
  }, _react.default.createElement(_components.Space, null, _react.default.createElement(_components2.DocTitle, null, method.summary), _react.default.createElement(_components2.DocSource, {
    method: method
  })), runItToggle), _react.default.createElement(_components.Space, {
    mb: "large",
    gap: "small"
  }, _react.default.createElement(_components2.DocStatus, {
    method: method
  }), _react.default.createElement(_components2.DocActivityType, {
    method: method
  }), _react.default.createElement(_components2.DocRateLimited, {
    method: method
  })), _react.default.createElement(_components3.DocOperation, {
    method: method
  }), _react.default.createElement(_components2.DocMarkdown, {
    source: method.description,
    specKey: specKey
  }), _react.default.createElement(_components2.DocSDKs, {
    api: api,
    method: method
  }), _react.default.createElement(_components3.DocRequestBody, {
    api: api,
    method: method
  }), _react.default.createElement(_components2.DocSdkUsage, {
    method: method
  }), _react.default.createElement(_components2.DocReferences, {
    typesUsed: seeTypes,
    api: api,
    specKey: specKey
  }), _react.default.createElement(_components2.DocResponses, {
    api: api,
    responses: method.responses
  }), _react.default.createElement(_components2.DocSchema, {
    object: method.schema
  })), value && _react.default.createElement(_components.Aside, {
    width: "50rem"
  }, _react.default.createElement(_components.ExtendComponentsThemeProvider, {
    themeCustomizations: {
      colors: {
        background: '#262D33',
        key: '#8AB4F8',
        text: '#fff',
        link: '#8AB4F8',
        critical: '#FF877C'
      }
    }
  }, _react.default.createElement(_runIt.RunIt, {
    adaptor: adaptor,
    key: method.operationId,
    sdkLanguage: sdkLanguage,
    api: api,
    method: method
  }))));
};

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