import React from 'react';

function BaseSelect({ options, onChange, defaultValue }) {
  const buildOption = option => <option
                                  value={option.id}
                                  key={option.id}>
                                  {option.displayName}
                                </option>;
  const sortBy = (x, y) => {
    if (x.position < y.position) {
      return -1;
    }
    if (x.position > y.position) {
      return 1;
    }
    return 0;
  }
  return (
    <div className='c-base-select'>
      <select key={defaultValue} onChange={onChange} defaultValue={defaultValue}>
        {options.sort(sortBy).map(buildOption)}
      </select>
    </div>
  );
};

export default BaseSelect;
