import extend from 'extend';
import React, { PropTypes } from 'react';

import { UnwrappedKdSelectEntity } from '../KdSelectEntity';
import wrap from '../ItemContainerWrapper';
import { ADD_CHICKLET } from '../../constants';

const defaultProps = {
  selectedValuesInitial: [],
  selectBehavior: ADD_CHICKLET,
  isDisabled: false,
  mainSelector: {
    visible: false,
    selected: '',
    optionValues: {
      anyOption: {
        visible: true,
        label: 'Any',
      },
      customOptions: [],
      userSpecifiedOption: {
        visible: true,
        label: 'Search...',
      },
    },
  },
  events: {},
  autoComplete: {
    clearTextboxOnSelection: true,
    renderClearTextboxButton: true,
  },
};

const propTypes = {
  selectBehavior: PropTypes.string,
  kdFilterModel: PropTypes.object.isRequired,

  api: PropTypes.shape({
    url: PropTypes.string.isRequired,
    key: PropTypes.string.isRequired,
  }),

  events: PropTypes.shape({
    onSelect: PropTypes.func,
    onChange: PropTypes.func,
  }),

  mainSelector: PropTypes.shape({
    visible: PropTypes.bool,
    selected: PropTypes.string,

    optionValues: PropTypes.shape({
      selectOption: PropTypes.shape({
        visible: PropTypes.bool,
        label: PropTypes.string,
      }),
      anyOption: PropTypes.shape({
        visible: PropTypes.bool,
        label: PropTypes.string,
      }),
      customOptions: PropTypes.array,
      userSpecifiedOption: PropTypes.shape({
        visible: PropTypes.bool,
        label: PropTypes.string,
      }),
    }),
  }),

  autoComplete: PropTypes.shape({
    onSuccess: PropTypes.func,
    onRender: PropTypes.func,
    onSelect: PropTypes.func,
    onFail: PropTypes.func,
    onDone: PropTypes.func,
    placeholderText: PropTypes.string,
    clearTextboxOnSelection: PropTypes.bool,
    renderClearTextboxButton: PropTypes.bool,
  }),

  selectedValuesInitial: PropTypes.array,
};

function KdAutocomplete(props) {
  const autocompleteProps = extend(true, {}, defaultProps, props);
  return <UnwrappedKdSelectEntity {...autocompleteProps} />;
}

KdAutocomplete.defaultProps = defaultProps;
KdAutocomplete.propTypes = propTypes;

export default wrap(KdAutocomplete);
