import { __assign, __read } from "tslib";
import React from 'react';
import { usePlatform } from '../../../utils/platform';
import FormCell from '../formcell';
import SelectH5, { decodeIsoDatetime } from './h5';
import classNames from '../../../utils/classnames';
import { Select as TeaSelect, DatePicker, TimePicker, Cascader, ConfigProvider, } from 'tea-component';
import moment from 'moment';
import isObjectEqual from '../../../utils/isObjectEqual';
import { getRegionTree } from './region';
export default function Select(props) {
    var platform = usePlatform();
    if (platform === 'h5') {
        return React.createElement(SelectH5, __assign({}, props));
    }
    return (React.createElement(ConfigProvider, { classPrefix: "wedatea2td" },
        React.createElement(SelectPc, __assign({}, props))));
}
function SelectPc(_a) {
    var _b;
    var id = _a.id, style = _a.style, className = _a.className, defaultValue = _a.defaultValue, placeholder = _a.placeholder, label = _a.label, labelVisible = _a.labelVisible, mode = _a.mode, events = _a.events, layout = _a.layout, disabled = _a.disabled, requiredFlag = _a.requiredFlag, range = _a.range, startDate = _a.startDate, endDate = _a.endDate, defaultDate = _a.defaultDate, defaultTime = _a.defaultTime, startTime = _a.startTime, endTime = _a.endTime, defaultRegion = _a.defaultRegion, separator = _a.separator, size = _a.size;
    var cls = classNames((_b = {
            'weda-ui': true,
            'weda-select': true
        },
        _b[className] = className,
        _b));
    // 两次默认值不同, 需要刷新
    var prevDefaultRef = React.useRef();
    var _c = __read(React.useState(decodeIsoDatetime(defaultDate, 'date')), 2), date = _c[0], setDate = _c[1];
    var _d = __read(React.useState(decodeIsoDatetime(defaultTime, 'time')), 2), time = _d[0], setTime = _d[1];
    var _e = __read(React.useState(defaultRegion || undefined), 2), region = _e[0], setRegion = _e[1];
    var _f = __read(React.useState(defaultValue), 2), selectedValue = _f[0], setSelectedValue = _f[1];
    React.useEffect(function () {
        var _a, _b, _c, _d;
        var hasChange = false;
        if (((_a = prevDefaultRef.current) === null || _a === void 0 ? void 0 : _a.defaultValue) !== defaultValue) {
            setSelectedValue(defaultValue);
            hasChange = true;
        }
        if (((_b = prevDefaultRef.current) === null || _b === void 0 ? void 0 : _b.defaultDate) !== defaultDate) {
            setDate(decodeIsoDatetime(defaultDate, 'date'));
            hasChange = true;
        }
        if (((_c = prevDefaultRef.current) === null || _c === void 0 ? void 0 : _c.defaultTime) !== defaultTime) {
            var ts = decodeIsoDatetime(defaultTime, 'time');
            setTime(ts);
            hasChange = true;
        }
        if (!isObjectEqual((_d = prevDefaultRef.current) === null || _d === void 0 ? void 0 : _d.defaultRegion, defaultRegion)) {
            setRegion(defaultRegion !== null && defaultRegion !== void 0 ? defaultRegion : []);
            hasChange = true;
        }
        if (hasChange) {
            prevDefaultRef.current = {
                defaultDate: defaultDate,
                defaultTime: defaultTime,
                defaultRegion: defaultRegion,
            };
        }
    });
    var onDataChange = function (value) {
        var finalVal;
        if (mode === 'date') {
            finalVal = value.valueOf();
        }
        else if (mode === 'time') {
            finalVal = (value.hour() * 60 * 60 + value.minute() * 60) * 1000;
        }
        else if (mode === 'region') {
            finalVal = value !== null && value !== void 0 ? value : [];
        }
        else {
            finalVal = value;
        }
        events.change({ value: finalVal });
    };
    var createDropdown = function () {
        switch (mode) {
            case 'selector':
                return (React.createElement(TeaSelect, { size: size, onChange: onDataChange, disabled: disabled, defaultValue: selectedValue, type: "simulate", appearance: "button", placeholder: placeholder, options: range.map(function (i) { return ({ text: i.label, value: i.value }); }) }));
            case 'date': {
                var now = moment();
                var start = startDate
                    ? decodeIsoDatetime(startDate, 'date')
                    : now.year() - 20 + "-01-01";
                var end = endDate
                    ? decodeIsoDatetime(endDate, 'date')
                    : now.year() + 20 + "-12-31";
                return (React.createElement(DatePicker, { range: [moment(start), moment(end)], onChange: onDataChange, disabled: disabled, placeholder: placeholder, value: date ? moment(date) : undefined }));
            }
            case 'time': {
                var start = startTime
                    ? decodeIsoDatetime(startTime, 'time')
                    : '00:00';
                var end = endTime ? decodeIsoDatetime(endTime, 'time') : '23:59';
                var now = moment();
                return (React.createElement(TimePicker, { onChange: onDataChange, disabled: disabled, placeholder: placeholder, value: time ? moment(time, 'HH:mm') : undefined, range: [moment(start, 'HH:mm'), moment(end, 'HH:mm')], format: "HH:mm" }));
            }
            case 'region':
                return (React.createElement(Cascader, { clearable: true, searchable: true, type: "menu", disabled: disabled, data: getRegionTree(), multiple: false, onChange: onDataChange, valueRender: function (options) {
                        return options.map(function (_a, index) {
                            var label = _a.label, value = _a.value;
                            return (React.createElement(React.Fragment, { key: value + "-" + index },
                                index !== 0 && "" + separator,
                                label || value));
                        });
                    }, value: region }));
        }
    };
    var dropdown = createDropdown();
    return (React.createElement(FormCell, { size: size, id: id, className: cls, style: style, label: labelVisible ? label : null, layout: layout, multiCell: false, requiredFlag: requiredFlag }, dropdown));
}
//# sourceMappingURL=index.js.map