var _tests$SELECT$tests$M;

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { action } from '@storybook/addon-actions';
import { boolean, radios } from '@storybook/addon-knobs';
import Screener, { Steps } from 'screener-storybook/src/screener.js';
import List from 'react-virtualized/dist/commonjs/List'; // eslint-disable-line import/extensions

import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer'; // eslint-disable-line import/extensions

import { StyledList } from '../menu/index.js';
import { StatefulSelect, StyledDropdownListItem, TYPE } from './index.js';
import { SIZE } from './constants.js';
import { styled } from '../styles/index.js';
import COLORS from './examples-colors.json';
import tests from './examples-list.js';

var size = function size() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : SIZE.default;
  return radios('size', {
    default: SIZE.default,
    compact: SIZE.compact
  }, defaultValue);
};

var type = function type() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : TYPE.select;
  return radios('type', {
    select: TYPE.select,
    search: TYPE.search
  }, defaultValue);
};

var backspaceRemoves = function backspaceRemoves() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('backspaceRemoves', defaultValue);
};

var clearable = function clearable() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('clearable', defaultValue);
};

var closeOnSelect = function closeOnSelect() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('closeOnSelect', defaultValue);
};

var deleteRemoves = function deleteRemoves() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('deleteRemoves', defaultValue);
};

var disabled = function disabled() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  return boolean('disabled', defaultValue);
};

var error = function error() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  return boolean('error', defaultValue);
};

var escapeClearsValue = function escapeClearsValue() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('escapeClearsValue', defaultValue);
};

var filterOutSelected = function filterOutSelected() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('filterOutSelected', defaultValue);
};

var isLoading = function isLoading() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  return boolean('isLoading', defaultValue);
};

var multi = function multi() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  return boolean('multi', defaultValue);
};

var onBlurResetsInput = function onBlurResetsInput() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('onBlurResetsInput', defaultValue);
};

var onCloseResetsInput = function onCloseResetsInput() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('onCloseResetsInput', defaultValue);
};

var onSelectResetsInput = function onSelectResetsInput() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('onSelectResetsInput', defaultValue);
};

var openOnClick = function openOnClick() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('openOnClick', defaultValue);
};

var required = function required() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('required', defaultValue);
};

var searchable = function searchable() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  return boolean('searchable', defaultValue);
};

var onChange = function onChange() {
  var _console;

  // eslint-disable-next-line no-console
  (_console = console).log.apply(_console, arguments);
};

var onChangeAction = action('select change');
var CustomValueWrapper = styled('span', {
  display: 'inline-flex',
  alignItems: 'center'
});
CustomValueWrapper.displayName = "CustomValueWrapper";
var CustomValueColor = styled('span', function (_ref) {
  var $color = _ref.$color;
  return {
    display: 'inline-block',
    borderRadius: '50%',
    width: '10px',
    height: '10px',
    backgroundColor: $color,
    marginRight: '5px'
  };
});
CustomValueColor.displayName = "CustomValueColor";

function CustomOptionLabel(_ref2) {
  var option = _ref2.option,
      _ref2$showColor = _ref2.showColor,
      showColor = _ref2$showColor === void 0 ? false : _ref2$showColor;
  return React.createElement(CustomValueWrapper, null, option.color ? React.createElement(CustomValueColor, {
    $color: option.color
  }) : null, option.id, showColor && " (".concat(option.color, ")"));
}

var options = {
  options: COLORS,
  labelKey: 'id',
  valueKey: 'color',
  onChange: onChange,
  placeholder: 'Choose a color',
  maxDropdownHeight: '300px'
};

var optionsWithDisabled = _toConsumableArray(COLORS);

optionsWithDisabled[1] = _objectSpread({}, optionsWithDisabled[1], {
  disabled: true
});
export default (_tests$SELECT$tests$M = {}, _defineProperty(_tests$SELECT$tests$M, tests.SELECT, function Story1() {
  return React.createElement(StatefulSelect, _extends({}, options, {
    backspaceRemoves: backspaceRemoves(),
    clearable: clearable(),
    closeOnSelect: closeOnSelect(),
    deleteRemoves: deleteRemoves(),
    disabled: disabled(),
    error: error(),
    escapeClearsValue: escapeClearsValue(),
    filterOutSelected: filterOutSelected(),
    isLoading: isLoading(),
    multi: multi(),
    onBlurResetsInput: onBlurResetsInput(),
    onCloseResetsInput: onCloseResetsInput(),
    onSelectResetsInput: onSelectResetsInput(),
    openOnClick: openOnClick(),
    required: required(),
    searchable: searchable(),
    size: size(),
    type: type()
  }, {
    onChange: onChangeAction,
    placeholder: "Check out the `KNOBS` tab to toggle some props" // eslint-disable-next-line jsx-a11y/no-autofocus
    ,
    autoFocus: false
  }));
}), _defineProperty(_tests$SELECT$tests$M, tests.MULTI_SELECT, function Story2() {
  return React.createElement(StatefulSelect, _extends({}, options, {
    initialState: {
      value: [_objectSpread({}, COLORS[0], {
        clearableValue: false
      }), COLORS[1], COLORS[3]]
    },
    closeOnSelect: false,
    clearable: false,
    multi: true,
    onChange: onChange
  }));
}), _defineProperty(_tests$SELECT$tests$M, tests.SINGLE_SELECT_SEARCH, function Story3() {
  var selector = '[role="list"] div';
  return React.createElement(Screener, {
    steps: new Steps().wait(selector).click(selector).snapshot('select open state').end()
  }, React.createElement(StatefulSelect, _extends({}, options, {
    options: optionsWithDisabled,
    placeholder: "Start searching",
    type: TYPE.search,
    onChange: onChange
  })));
}), _defineProperty(_tests$SELECT$tests$M, tests.MULTI_SELECT_SEARCH, function Story4() {
  return React.createElement(StatefulSelect, _extends({}, options, {
    placeholder: "Start searching",
    type: TYPE.search,
    multi: true,
    onChange: onChange
  }));
}), _defineProperty(_tests$SELECT$tests$M, tests.MULTI_SELECT_CUSTOM_LABELS, function Story5() {
  return React.createElement(StatefulSelect, _extends({}, options, {
    initialState: {
      value: [COLORS[2]]
    },
    type: TYPE.search,
    multi: true,
    onChange: onChange,
    getOptionLabel: function getOptionLabel(_ref3) {
      var option = _ref3.option;
      return React.createElement(CustomOptionLabel, {
        option: option,
        showColor: true
      });
    },
    getValueLabel: function getValueLabel(_ref4) {
      var option = _ref4.option;
      return React.createElement(CustomOptionLabel, {
        option: option
      });
    },
    overrides: {
      DropdownListItem: {
        style: function style(_ref5) {
          var $theme = _ref5.$theme,
              $isHighlighted = _ref5.$isHighlighted;
          return {
            backgroundColor: $isHighlighted ? $theme.colors.primary50 : 'transparent',
            ':hover': {
              backgroundColor: $theme.colors.primary50
            }
          };
        }
      }
    }
  }));
}), _defineProperty(_tests$SELECT$tests$M, tests.LONG_LIST, function LongListStory() {
  var ListItem = styled(StyledDropdownListItem, {
    paddingTop: 0,
    paddingBottom: 0,
    display: 'flex',
    alignItems: 'center'
  });
  ListItem.displayName = "ListItem";
  var Container = styled(StyledList, {
    height: '500px'
  });
  Container.displayName = "Container";

  function VirtualList(props) {
    return React.createElement(Container, {
      $ref: props.$ref
    }, React.createElement(AutoSizer, null, function (_ref6) {
      var width = _ref6.width;
      return React.createElement(List, {
        role: props.role,
        height: 500,
        width: width,
        rowCount: props.children.length,
        rowHeight: 36,
        rowRenderer: function rowRenderer(_ref7) {
          var index = _ref7.index,
              key = _ref7.key,
              style = _ref7.style;
          return React.createElement(ListItem, _extends({
            key: key,
            style: style
          }, props.children[index].props), props.children[index].props.item.id);
        }
      });
    }));
  }

  return React.createElement(StatefulSelect, _extends({}, options, {
    overrides: {
      Dropdown: {
        component: VirtualList
      }
    }
  }));
}), _tests$SELECT$tests$M);