"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Tools = void 0;
var _react = _interopRequireWildcard(require("react"));
var _components = require("@storybook/components");
var _Icons = require("./Icons");
var _qs = require("qs");
var _theming = require("@storybook/theming");
var _types = require("../../../types");
var _helpers = require("../../shared/helpers");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const Button = (0, _theming.styled)(_components.IconButton)({
  '&:disabled': {
    opacity: 0.5,
    cursor: 'default'
  },
  '&:disabled:hover': {
    color: 'inherit'
  }
});
const Tools = _ref => {
  let {
    manager
  } = _ref;
  const [buttonClicked, setButtonClicked] = (0, _react.useState)();
  const [isRunning, setRunning] = (0, _react.useState)(manager.status.isRunning);
  const forceUpdate = (0, _helpers.useForceUpdate)();
  const test = manager.getCurrentTest();
  (0, _react.useEffect)(() => {
    const unsubscribe = manager.onChangeTest(() => {
      forceUpdate();
    });
    return unsubscribe;
  }, [manager, forceUpdate]);
  (0, _react.useEffect)(() => {
    const unsubscribe = manager.onUpdateStatus(_ref2 => {
      let {
        isRunning
      } = _ref2;
      if ((0, _types.isDefined)(isRunning)) setRunning(isRunning);
    });
    return unsubscribe;
  }, [manager]);
  if (!test) return null;
  function renderButton(type, title, onClick, icon) {
    const handleClick = () => {
      setButtonClicked(type);
      onClick();
    };
    const disabled = isRunning && buttonClicked != null && buttonClicked !== type;
    return (
      /*#__PURE__*/
      // @ts-expect-error Fixed in https://github.com/storybookjs/storybook/pull/26623
      _react.default.createElement(Button, {
        onClick: () => {
          isRunning ? manager.onStop() : handleClick();
        },
        title: disabled ? '' : title,
        disabled: disabled
      }, buttonClicked === type && isRunning ? /*#__PURE__*/_react.default.createElement(_components.Icons, {
        icon: 'stop'
      }) : icon)
    );
  }
  return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement(_components.IconButton, {
    href: `http://localhost:${__CREEVEY_CLIENT_PORT__ || __CREEVEY_SERVER_PORT__ || 3000}/?${(0, _qs.stringify)({
      testPath: (0, _helpers.getTestPath)(test)
    })}`,
    target: "_blank",
    title: "Show in Creevey UI"
  }, /*#__PURE__*/_react.default.createElement(_components.Icons, {
    icon: "sharealt"
  })), /*#__PURE__*/_react.default.createElement(_components.Separator, null), renderButton('RunAll', 'Run all', manager.onStartAllTests, /*#__PURE__*/_react.default.createElement(_Icons.ForwardIcon, null)), renderButton('RunStoryTests', 'Run all story tests', manager.onStartAllStoryTests, /*#__PURE__*/_react.default.createElement(_Icons.NextIcon, {
    width: 15,
    height: 11
  })), renderButton('RunTest', 'Run', manager.onStart, /*#__PURE__*/_react.default.createElement(_components.Icons, {
    icon: "play"
  })));
};
exports.Tools = Tools;