content: 'use strict';
  
  /*
   * Module Definition
   *
   */
  
  import React from 'react';
  import { isEqual, scrollToComponent } from '../utils/index';
  import ResultsDialog from './presentational/ResultsDialog';
  import ResultsListItem from './presentational/ResultsListItem';
  
  /*
   * Class Definition
   *
   */
  
  class ResultList extends React.Component {
    constructor(props) {
      super(props);
  
      this.onClick = this.onClick.bind(this);
    }
  
    /*
     * onClick
     *
     */
  
    componentDidUpdate(prevProps, prevState) {
      const isSelectedEntityChanged = !isEqual(prevProps.selectedEntity, this.props.selectedEntity);
      isSelectedEntityChanged && this.scrollTo();
    }
  
    /*
     * onClick
     *
     */
  
    onClick(idx, evt) {
      const href = evt.target.getAttribute('href');
  
      if (!href) {
        evt.preventDefault();
        this.scrollTo(evt.currentTarget);
        this.props.setSelectedEntity(idx);
      }
    }
  
    /*
     * scrollTo
     *
     */
  
    scrollTo() {
      if (this.props.selectedEntity === null) return; 
  
      const $list = $('.dealer-list');
      const $item = $('li.active', $list);
      const itemTop = $item.position().top + $list.scrollTop();
  
      $list.animate({ scrollTop : itemTop });
    }  
  
    /*
     * render
     *
     */
  
    render() {    
      const ResultItems = () => {      
        return (
          <div className="dealer-list">
            <ul>
              {this.props.entities.items.map((item, idx) => <ResultsListItem
                key={ idx }
                idx={ idx }
                item={ item }
                onClick={ this.onClick } 
                selectedEntity={ this.props.selectedEntity } />)}
            </ul>
          </div>
        )
      };
  
      return (
        <div className="col-xs-12 col-sm-4 col-sm-pull-8 col-md-3 col-md-pull-9 col-trim-padding">
          { this.props.itemsCount ? <ResultItems /> : <ResultsDialog /> }
        </div>
      )
    }
  }
  
  /*
   * Module Export
   *
   */
  
  export default ResultList;
  