"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.withMultilineLabel = exports.withLoadingSpinner = exports.withCustomLink = exports.withCustomAction = exports.withCloseButton = exports.default = exports.DefaultStory = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

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

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

var _index = require("../../index");

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var _default = {
  component: _index.Toast,
  title: (0, _utils.addStoryInGroup)(_utils.MID_LEVEL_BLOCKS, 'Toast'),
  parameters: {
    docs: {
      source: {
        type: 'code'
      }
    }
  }
};
exports.default = _default;

var toastReducer = function toastReducer(_ref, action) {
  var toasts = _ref.toasts,
      key = _ref.key;

  if (action.type === 'REMOVE_TOAST') {
    return {
      key: key,
      toasts: toasts.filter(function (t) {
        return t.key !== action.payload;
      })
    };
  }

  if (action.type === 'ADD_TOAST') {
    return {
      key: key + 1,
      toasts: toasts.concat([action.payload])
    };
  }

  return {
    key: key,
    toasts: toasts
  };
};

var toastSpawner = function toastSpawner(toastProps) {
  var Wrapper = function Wrapper() {
    var _useReducer = (0, _react.useReducer)(toastReducer, {
      key: 0,
      toasts: []
    }),
        _useReducer2 = (0, _slicedToArray2.default)(_useReducer, 2),
        _useReducer2$ = _useReducer2[0],
        toasts = _useReducer2$.toasts,
        key = _useReducer2$.key,
        dispatch = _useReducer2[1];

    var addToast = function addToast(toastProps) {
      var props = _objectSpread(_objectSpread({}, toastProps), {}, {
        key: key,
        onClose: function onClose() {
          return dispatch({
            type: 'REMOVE_TOAST',
            payload: key
          });
        },
        onTimeout: function onTimeout() {
          return dispatch({
            type: 'REMOVE_TOAST',
            payload: key
          });
        }
      });

      dispatch({
        type: 'ADD_TOAST',
        payload: props
      });
    };

    return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_index.Button, {
      label: "Make a toast",
      onClick: function onClick() {
        return addToast(toastProps);
      }
    }), /*#__PURE__*/_react.default.createElement(_index.ToastContainer, null, toasts.map(function (toastProps) {
      return /*#__PURE__*/_react.default.createElement(_index.Toast, (0, _extends2.default)({}, toastProps, {
        key: toastProps.key
      }));
    })));
  };

  return Wrapper;
};

var DefaultStory = function DefaultStory(args) {
  return toastSpawner(args)();
};

exports.DefaultStory = DefaultStory;
var withCloseButton = toastSpawner({
  label: 'Toast label',
  timeout: 3000
});
exports.withCloseButton = withCloseButton;
withCloseButton.storyName = 'With close button';
var withCustomAction = toastSpawner({
  label: 'Toast label',
  timeout: 3000,
  actionLabel: 'confirm',
  action: function action() {
    return true;
  }
});
exports.withCustomAction = withCustomAction;
withCustomAction.storyName = 'With custom action';
var withCustomLink = toastSpawner({
  label: 'Toast label',
  link: /*#__PURE__*/_react.default.createElement(_index.Link, {
    href: "https://www.teamleader.be"
  }, "link"),
  timeout: 3000
});
exports.withCustomLink = withCustomLink;
withCustomLink.storyName = 'With custom link';
var withMultilineLabel = toastSpawner({
  label: 'Connection timed out. Showing limited amount of messages.',
  timeout: 3000,
  actionLabel: 'Try again',
  action: function action() {
    return true;
  }
});
exports.withMultilineLabel = withMultilineLabel;
withMultilineLabel.storyName = 'With multiline label';
var withLoadingSpinner = toastSpawner({
  label: 'Working...',
  timeout: 3000,
  processing: true
});
exports.withLoadingSpinner = withLoadingSpinner;
withLoadingSpinner.storyName = 'With loading spinner';