import React from 'react';
import SingleSelect from '@nestio/pam/lib/components/SingleSelect';
import MultiSelect from '@nestio/pam/lib/components/MultiSelect';
import CalendarSelect from '@nestio/pam/lib/components/CalendarSelect';
import enhanceWithInputEl from '@nestio/pam/lib/decorators/enhanceWithInputEl';
import { DATE_FORMAT_DISPLAY } from '../../constants/dateTimeFormats';
import moment from 'moment';

const processMomentValue = momentValue => {
    if (moment.isMoment(momentValue)) {
        return momentValue.format(DATE_FORMAT_DISPLAY);
    }
    if (moment(momentValue).isValid()) {
        return moment(momentValue).format(DATE_FORMAT_DISPLAY);
    }

    return momentValue;
};

const renderSingleSelectValueLabelWithPlaceHolder = (placeholder = 'Select One') => (option) => {
    if (!option) {
        return (
            <span className="placeholder">{placeholder}</span>
        );
    }

    return (
        <span>{option.label}</span>
    );
};

export const renderMultiSelectValueLabelWithPlaceHolder = (placeholder = 'Select One') => (options) => {
    if (!options || options.length === 0) {
        return (
            <span className="placeholder">{placeholder}</span>
        );
    }

    return (
        <span>{`${options.length} selected`}</span>
    );
};

export const InputSingleSelect = enhanceWithInputEl(props => {
    return (
        <SingleSelect
            renderValueLabel={renderSingleSelectValueLabelWithPlaceHolder(props.placeholder)}
            {...props}
        />
    );
});

export const InputMultiSelect = enhanceWithInputEl(props => {
    return (
        <MultiSelect
            renderValueLabel={renderMultiSelectValueLabelWithPlaceHolder(props.placeholder)}
            {...props}
        />
    );
});

export const InputCalendarSelect = enhanceWithInputEl(CalendarSelect, processMomentValue);
