"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MixinsStackee = exports.MixinsSpacing = exports.ClearButton = exports.Description = exports.ErrorMessage = exports.Error = exports.Success = exports.DeprecatedHiddenLabel = exports.ClearOnResultClick = exports.FocusOnMount = exports.CustomNotFoundOnNoResults = exports.CustomItemRendererAndSeparators = exports.NotFoundButton = exports.InitialValue = exports.Default = exports["default"] = void 0;

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _lodash = require("lodash");

var _addonActions = require("@storybook/addon-actions");

var _searchInput = require("./search-input.component");

var _primitives = require("../../../primitives");

var _typography = require("../../typography");

var _branding = require("../../../mixins/branding");

var _paddingChecker = require("../../../mixins/spacing/story-support/padding-checker");

var _marginChecker = require("../../../mixins/spacing/story-support/margin-checker");

var _stackeeChecker = require("../../../mixins/stackee/story-support/stackee-checker");

var _searchInput2 = _interopRequireDefault(require("./search-input.mdx"));

var _link = require("../../typography/link");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var _default = {
  component: _searchInput.SearchInput,
  title: 'Components/Inputs/SearchInput',
  parameters: {
    docs: {
      page: _searchInput2["default"]
    }
  }
};
exports["default"] = _default;

var SearchInputDemo = function SearchInputDemo(_ref) {
  var _ref$allowNotFound = _ref.allowNotFound,
      allowNotFound = _ref$allowNotFound === void 0 ? false : _ref$allowNotFound,
      _ref$showNotFoundOnly = _ref.showNotFoundOnlyOnNoResults,
      showNotFoundOnlyOnNoResults = _ref$showNotFoundOnly === void 0 ? false : _ref$showNotFoundOnly,
      iconPosition = _ref.iconPosition,
      _ref$notFoundText = _ref.notFoundText,
      notFoundText = _ref$notFoundText === void 0 ? '' : _ref$notFoundText,
      value = _ref.value,
      initialValue = _ref.initialValue,
      renderItem = _ref.renderItem,
      renderNotFoundItem = _ref.renderNotFoundItem,
      _ref$focusOnMount = _ref.focusOnMount,
      focusOnMount = _ref$focusOnMount === void 0 ? false : _ref$focusOnMount,
      _ref$showSeparators = _ref.showSeparators,
      showSeparators = _ref$showSeparators === void 0 ? false : _ref$showSeparators,
      _ref$clearSearchOnCli = _ref.clearSearchOnClick,
      clearSearchOnClick = _ref$clearSearchOnCli === void 0 ? false : _ref$clearSearchOnCli,
      _ref$error = _ref.error,
      error = _ref$error === void 0 ? false : _ref$error,
      success = _ref.success,
      description = _ref.description,
      clearLabel = _ref.clearLabel,
      onClear = _ref.onClear,
      _ref$deprecatedHidden = _ref.deprecatedHiddenLabel,
      deprecatedHiddenLabel = _ref$deprecatedHidden === void 0 ? false : _ref$deprecatedHidden;

  var _useState = (0, _react.useState)(),
      _useState2 = _slicedToArray(_useState, 2),
      demoSearchInput = _useState2[0],
      setDemoSearchInput = _useState2[1];

  var onDemoSearchInputChange = (0, _react.useCallback)(function (newSearchInput) {
    (0, _addonActions.action)('Setting search term')(newSearchInput);
    setDemoSearchInput(newSearchInput);
  }, []);
  var onDemoSearchResultClick = (0, _react.useCallback)(function (id, v) {
    (0, _addonActions.action)('Clicked ID')(id);
    (0, _addonActions.action)('Setting value')(v);
  }, []);
  var countries = [{
    id: 'country-can',
    text: 'Canada',
    region: 'North America'
  }, {
    id: 'country-usa',
    text: 'United States',
    region: 'North America'
  }, {
    id: 'country-uae',
    text: 'United Arab Emirates',
    region: 'Middle East'
  }, {
    id: 'country-fr',
    text: 'France',
    region: 'Europe'
  }, {
    id: 'country-es',
    text: 'Spain',
    region: 'Europe'
  }];

  var search = function search(searchTerm) {
    if (searchTerm == null) {
      return [];
    }

    var regexp = new RegExp(searchTerm, 'gi');
    var results = countries.filter(function (country) {
      return country.text.match(regexp);
    });

    if (showSeparators) {
      var resultsByRegion = (0, _lodash.groupBy)(results, 'region');
      results = [];
      Object.keys(resultsByRegion).forEach(function (key) {
        var _resultsByRegion$key;

        var resultsForRegion = (_resultsByRegion$key = resultsByRegion[key]) !== null && _resultsByRegion$key !== void 0 ? _resultsByRegion$key : [];

        if (resultsForRegion.length > 0) {
          var _results;

          results.push({
            id: key,
            text: key,
            isSeparator: true
          });

          (_results = results).push.apply(_results, _toConsumableArray(resultsForRegion));
        }
      });
    }

    return results;
  };

  return /*#__PURE__*/_react["default"].createElement(_searchInput.SearchInput, {
    value: value,
    initialValue: initialValue,
    iconPosition: iconPosition,
    placeholder: "Placeholder",
    onChange: onDemoSearchInputChange,
    onSearchResultClicked: onDemoSearchResultClick,
    searchResults: search(demoSearchInput),
    searchInput: demoSearchInput,
    allowNotFound: allowNotFound,
    showNotFoundOnlyOnNoResults: showNotFoundOnlyOnNoResults,
    notFoundText: notFoundText,
    label: "Country search",
    renderItem: renderItem,
    renderNotFoundItem: renderNotFoundItem,
    focusOnMount: focusOnMount,
    clearSearchOnClick: clearSearchOnClick,
    error: error,
    success: success,
    description: description,
    clearLabel: clearLabel,
    onClear: onClear,
    labelStyle: deprecatedHiddenLabel ? 'hidden' : undefined
  });
};

var Default = function Default() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, null);
};

exports.Default = Default;

var InitialValue = function InitialValue() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    initialValue: "Canada"
  });
};

exports.InitialValue = InitialValue;

var NotFoundButton = function NotFoundButton() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    allowNotFound: true,
    notFoundText: "Can't find it?"
  });
};

exports.NotFoundButton = NotFoundButton;
var HoverBox = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "HoverBox",
  componentId: "c18c51-0"
})(["background:", ";&:hover,&:focus,&:active{background:", ";color:", ";}"], function (_ref2) {
  var selected = _ref2.selected,
      theme = _ref2.theme;
  return selected ? (0, _branding.getPaletteColor)('brand-secondary-bg')({
    theme: theme
  }) : (0, _branding.getPaletteColor)('white')({
    theme: theme
  });
}, (0, _branding.getPaletteColor)('brand-secondary-bg'), (0, _branding.getPaletteColor)('brand-secondary-bg'));

var CustomItemRendererAndSeparators = function CustomItemRendererAndSeparators() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    showSeparators: true,
    renderItem: function renderItem(_ref3) {
      var id = _ref3.id,
          text = _ref3.text,
          selected = _ref3.selected;
      return /*#__PURE__*/_react["default"].createElement(HoverBox, {
        selected: selected,
        spacing: ['ph2', 'p1']
      }, /*#__PURE__*/_react["default"].createElement(_typography.Text, {
        spacing: "m0"
      }, "ID: ", id), /*#__PURE__*/_react["default"].createElement(_typography.Text, {
        bold: true,
        spacing: "m0",
        fontSize: "f4"
      }, text));
    }
  });
};

exports.CustomItemRendererAndSeparators = CustomItemRendererAndSeparators;

var CustomNotFoundOnNoResults = function CustomNotFoundOnNoResults() {
  var NotFoundItem = /*#__PURE__*/_react["default"].createElement(_primitives.Box, {
    stacked: "column",
    spacing: ['ph2', 'pv4']
  }, /*#__PURE__*/_react["default"].createElement(_typography.Text, {
    spacing: "m0",
    align: "center"
  }, "Can't find what you're looking for?"), /*#__PURE__*/_react["default"].createElement(_typography.Text, {
    align: "center"
  }, /*#__PURE__*/_react["default"].createElement(_link.Link, {
    colorName: "brand-secondary-fg",
    onClick: (0, _addonActions.action)('findOutWhyClicked')
  }, "Find out why")));

  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    allowNotFound: true,
    showNotFoundOnlyOnNoResults: true,
    renderNotFoundItem: function renderNotFoundItem() {
      return NotFoundItem;
    }
  });
};

exports.CustomNotFoundOnNoResults = CustomNotFoundOnNoResults;

var FocusOnMount = function FocusOnMount() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    focusOnMount: true
  });
};

exports.FocusOnMount = FocusOnMount;

var ClearOnResultClick = function ClearOnResultClick() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    clearSearchOnClick: true
  });
};

exports.ClearOnResultClick = ClearOnResultClick;

var DeprecatedHiddenLabel = function DeprecatedHiddenLabel() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    clearSearchOnClick: true,
    deprecatedHiddenLabel: true
  });
};

exports.DeprecatedHiddenLabel = DeprecatedHiddenLabel;

var Success = function Success() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    success: "Success message",
    initialValue: "Canada"
  });
};

exports.Success = Success;

var Error = function Error() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    error: true,
    initialValue: "Lilliput"
  });
};

exports.Error = Error;

var ErrorMessage = function ErrorMessage() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    error: "Error message",
    initialValue: "Lilliput"
  });
};

exports.ErrorMessage = ErrorMessage;

var Description = function Description() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    description: "Info/requirements message"
  });
};

exports.Description = Description;

var ClearButton = function ClearButton() {
  return /*#__PURE__*/_react["default"].createElement(SearchInputDemo, {
    clearLabel: "clear",
    onClear: (0, _addonActions.action)('onClear')
  });
};

exports.ClearButton = ClearButton;

var MixinsSpacing = function MixinsSpacing() {
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_paddingChecker.PaddingChecker, {
    Component: _searchInput.SearchInput,
    label: "Find stuff",
    searchResults: [{
      id: 'country-can',
      text: 'Canada'
    }, {
      id: 'country-usa',
      text: 'United States'
    }]
  }), /*#__PURE__*/_react["default"].createElement(_marginChecker.MarginChecker, {
    Component: _searchInput.SearchInput,
    label: "Find stuff",
    searchResults: [{
      id: 'country-can',
      text: 'Canada'
    }, {
      id: 'country-usa',
      text: 'United States'
    }]
  }));
};

exports.MixinsSpacing = MixinsSpacing;

var MixinsStackee = function MixinsStackee() {
  return /*#__PURE__*/_react["default"].createElement(_stackeeChecker.StackeeChecker, {
    Component: _searchInput.SearchInput,
    label: "Find stuff",
    searchResults: [{
      id: 'country-can',
      text: 'Canada'
    }, {
      id: 'country-usa',
      text: 'United States'
    }]
  });
};

exports.MixinsStackee = MixinsStackee;