import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

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) { _defineProperty(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; }

import React, { useReducer } from 'react';
import { addStoryInGroup, MID_LEVEL_BLOCKS } from '../../../.storybook/utils';
import { Button, Link, Toast, ToastContainer } from '../../index';
export default {
  component: Toast,
  title: addStoryInGroup(MID_LEVEL_BLOCKS, 'Toast'),
  parameters: {
    docs: {
      source: {
        type: 'code'
      }
    }
  }
};

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 = useReducer(toastReducer, {
      key: 0,
      toasts: []
    }),
        _useReducer2 = _slicedToArray(_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.createElement("div", null, /*#__PURE__*/React.createElement(Button, {
      label: "Make a toast",
      onClick: function onClick() {
        return addToast(toastProps);
      }
    }), /*#__PURE__*/React.createElement(ToastContainer, null, toasts.map(function (toastProps) {
      return /*#__PURE__*/React.createElement(Toast, _extends({}, toastProps, {
        key: toastProps.key
      }));
    })));
  };

  return Wrapper;
};

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