"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useAutomationMenu = exports.AutomationMenuContextContainer = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _manualTriggersContainer = require("../../manual-triggers-container");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } // This is another layer on top of the AFE ManualRulesContainer.
// It is necessary that this container wraps the entire "automation" component and not nested. Else the user input form will not render correctly.
// This container provides an automation context to all children components through the useAutomationMenu hook.
var AutomationMenuContext = /*#__PURE__*/(0, _react.createContext)(undefined);
var AutomationMenuProvider = function AutomationMenuProvider(_ref) {
  var children = _ref.children,
    value = _ref.value;
  return /*#__PURE__*/_react.default.createElement(AutomationMenuContext.Provider, {
    value: value
  }, children);
};

// Hook that can be used anywhere under the AutomationMenuContextContainer to access the AutomationMenuContext values
var useAutomationMenu = exports.useAutomationMenu = function useAutomationMenu() {
  var context = (0, _react.useContext)(AutomationMenuContext);
  if (context === undefined) {
    throw new Error('useAutomationMenu must be used within a AutomationMenuProvider');
  }
  return context;
};
var AutomationMenuContextContainer = exports.AutomationMenuContextContainer = function AutomationMenuContextContainer(_ref2) {
  var baseAutomationUrl = _ref2.baseAutomationUrl,
    analyticsSource = _ref2.analyticsSource,
    objectAri = _ref2.objectAri,
    siteAri = _ref2.siteAri,
    canManageAutomation = _ref2.canManageAutomation,
    children = _ref2.children,
    emptyStateDescription = _ref2.emptyStateDescription,
    emptyStateAdminDescription = _ref2.emptyStateAdminDescription,
    _onRuleInvocationSuccess = _ref2.onRuleInvocationSuccess,
    _onRuleInvocationFailure = _ref2.onRuleInvocationFailure,
    onRuleInvocationLifecycleDone = _ref2.onRuleInvocationLifecycleDone;
  var _useState = (0, _react.useState)('NONE'),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    ruleExecutionState = _useState2[0],
    setRuleExecutionState = _useState2[1];
  return /*#__PURE__*/_react.default.createElement(_manualTriggersContainer.ManualRulesContainer, {
    site: siteAri,
    query: {
      objects: [objectAri]
    },
    onRuleInvocationLifecycleStarted: function onRuleInvocationLifecycleStarted() {
      setRuleExecutionState('NONE');
    },
    onRuleInvocationLifecycleDone: onRuleInvocationLifecycleDone,
    onRuleInvocationSuccess: function onRuleInvocationSuccess() {
      setRuleExecutionState('SUCCEED');
      _onRuleInvocationSuccess === null || _onRuleInvocationSuccess === void 0 || _onRuleInvocationSuccess();
    },
    onRuleInvocationFailure: function onRuleInvocationFailure() {
      setRuleExecutionState('FAILURE');
      _onRuleInvocationFailure === null || _onRuleInvocationFailure === void 0 || _onRuleInvocationFailure();
    }
  }, function (props) {
    var contextValue = _objectSpread(_objectSpread({}, props), {}, {
      fetchError: props.error,
      analyticsSource: analyticsSource,
      objectAri: objectAri,
      baseAutomationUrl: baseAutomationUrl,
      canManageAutomation: canManageAutomation,
      emptyStateDescription: emptyStateDescription,
      emptyStateAdminDescription: emptyStateAdminDescription,
      ruleExecutionState: ruleExecutionState
    });
    return /*#__PURE__*/_react.default.createElement(AutomationMenuProvider, {
      value: contextValue
    }, children());
  });
};