import React from 'react';
import { JDalign, JDbutton } from '../..';
import { IUseSelect } from '../../hooks/hook';
import { lastOf } from '../../utils/lastOf';
import { IJDalignProp } from '../components';
import JDtypho, { IJDtyphoProp } from '../typho/Typho';
import JDselect from './SelectBox';

export interface ISelectCountProp extends IJDalignProp {
  selectHook: IUseSelect;
  label?: string;
  labelProp?: IJDtyphoProp;
}

export const SelectCounter: React.FC<ISelectCountProp> = ({
  labelProp,
  label,
  selectHook,
  ...props
}) => {
  const { options = [], selectedOption, onChange } = selectHook;
  const count = selectedOption?.value || 0;
  const maxCount = lastOf(options)?.value || 0;

  const handleCount = (plus: boolean) => {
    console.log('count');
    console.log(count);
    onChange(options[count + (plus ? 1 : -1)]);
  };

  return (
    <JDalign
      className="counter"
      flex={{
        vCenter: true,
      }}
      {...props}
    >
      {label && (
        <JDtypho weight={600} mr="large" {...labelProp}>
          {label}
        </JDtypho>
      )}
      <JDalign flex className="counter__inner">
        <JDbutton
        
          disabled={count === 0}
          thema="grey1"
          
          mode="flat"
          className="counter__btn"
          onClick={() => {
            handleCount(false);
          }}
        >
          -
        </JDbutton>
        <JDselect mr="no" mb="no" {...selectHook} />
        <JDbutton
          disabled={maxCount <= count}
          thema="grey1"
          mode="flat"
          className="counter__btn"
          onClick={() => {
            handleCount(true);
          }}
        >
          +
        </JDbutton>
      </JDalign>
    </JDalign>
  );
};

export default SelectCounter;
