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

import React, { useState, useEffect } from 'react';
import { Aside, Button, ButtonOutline, Space, useToggle, ExtendComponentsThemeProvider } from '@looker/components';
import { Beaker } from '@looker/icons';
import { useHistory, useParams } from 'react-router-dom';
import { RunIt, RunItFormKey } from '@looker/run-it';
import { typeRefs } from '@looker/sdk-codegen';
import { useSelector } from 'react-redux';
import { getApixAdaptor } from '../../utils';
import { ApixSection, DocActivityType, DocMarkdown, DocRateLimited, DocReferences, DocResponses, DocSDKs, DocSdkUsage, DocSource, DocStatus, DocTitle, DocSchema } from '../../components';
import { selectSdkLanguage } from '../../state';
import { DocOperation, DocRequestBody } from './components';

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

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

export var MethodScene = _ref2 => {
  var {
    api
  } = _ref2;
  var adaptor = getApixAdaptor();
  var history = useHistory();
  var sdkLanguage = useSelector(selectSdkLanguage);
  var {
    specKey,
    methodTag,
    methodName
  } = useParams();
  var {
    value,
    toggle,
    setOn
  } = useToggle();
  var [method, setMethod] = useState(api.methods[methodName]);
  var seeTypes = typeRefs(api, method === null || method === void 0 ? void 0 : method.customTypes);
  var RunItButton = value ? Button : ButtonOutline;
  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]);
  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.createElement(RunItButton, {
    color: value ? 'key' : 'neutral',
    iconBefore: React.createElement(Beaker, null),
    onClick: toggle
  }, "Run It");
  return React.createElement(React.Fragment, null, method && React.createElement(ApixSection, null, React.createElement(Space, {
    between: true
  }, React.createElement(Space, null, React.createElement(DocTitle, null, method.summary), React.createElement(DocSource, {
    method: method
  })), runItToggle), React.createElement(Space, {
    mb: "large",
    gap: "small"
  }, React.createElement(DocStatus, {
    method: method
  }), React.createElement(DocActivityType, {
    method: method
  }), React.createElement(DocRateLimited, {
    method: method
  })), React.createElement(DocOperation, {
    method: method
  }), React.createElement(DocMarkdown, {
    source: method.description,
    specKey: specKey
  }), React.createElement(DocSDKs, {
    api: api,
    method: method
  }), React.createElement(DocRequestBody, {
    api: api,
    method: method
  }), React.createElement(DocSdkUsage, {
    method: method
  }), React.createElement(DocReferences, {
    typesUsed: seeTypes,
    api: api,
    specKey: specKey
  }), React.createElement(DocResponses, {
    api: api,
    responses: method.responses
  }), React.createElement(DocSchema, {
    object: method.schema
  })), value && React.createElement(Aside, {
    width: "50rem"
  }, React.createElement(ExtendComponentsThemeProvider, {
    themeCustomizations: {
      colors: {
        background: '#262D33',
        key: '#8AB4F8',
        text: '#fff',
        link: '#8AB4F8',
        critical: '#FF877C'
      }
    }
  }, React.createElement(RunIt, {
    adaptor: adaptor,
    key: method.operationId,
    sdkLanguage: sdkLanguage,
    api: api,
    method: method
  }))));
};
//# sourceMappingURL=MethodScene.js.map