import { __assign, __read } from "tslib";
import * as React from 'react';
import dayjs from 'dayjs';
import weui from '../../../utils/weui';
import classNames from '../../../utils/classnames';
import isObjectEqual from '../../../utils/isObjectEqual';
import FormCell from '../formcell';
import { timePicker } from './time';
import { regionPicker } from './region';
export default function Select(_a) {
    var _b;
    var _c;
    var 
    // 系统属性
    id = _a.id, className = _a.className, style = _a.style, events = _a.events, 
    // 组件属性
    label = _a.label, labelVisible = _a.labelVisible, name = _a.name, placeholder = _a.placeholder, layout = _a.layout, mode = _a.mode, _d = _a.disabled, disabled = _d === void 0 ? false : _d, requiredFlag = _a.requiredFlag, 
    // selector mode
    range = _a.range, defaultValue = _a.defaultValue, 
    // date mode
    startDate = _a.startDate, endDate = _a.endDate, defaultDate = _a.defaultDate, 
    // time mode
    startTime = _a.startTime, endTime = _a.endTime, defaultTime = _a.defaultTime, 
    // region mode
    defaultRegion = _a.defaultRegion, separator = _a.separator;
    var _e = __read(React.useState(decodeIsoDatetime(defaultDate, 'date')), 2), date = _e[0], setDate = _e[1];
    var _f = __read(React.useState(decodeIsoDatetime(defaultTime, 'time')), 2), time = _f[0], setTime = _f[1];
    var _g = __read(React.useState(defaultRegion), 2), region = _g[0], setRegion = _g[1];
    var _h = __read(React.useState(defaultValue), 2), selectedValue = _h[0], setSelectedValue = _h[1];
    // 两次默认值不同, 需要刷新
    var prevDefaultRef = React.useRef();
    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) {
            setTime(decodeIsoDatetime(defaultTime, 'time'));
            hasChange = true;
        }
        if (!isObjectEqual((_d = prevDefaultRef.current) === null || _d === void 0 ? void 0 : _d.defaultRegion, defaultRegion)) {
            setRegion((defaultRegion === null || defaultRegion === void 0 ? void 0 : defaultRegion.join(separator)) || '');
            hasChange = true;
        }
        if (hasChange) {
            prevDefaultRef.current = {
                defaultDate: defaultDate,
                defaultTime: defaultTime,
                defaultRegion: defaultRegion,
            };
        }
    });
    var isFlex = layout !== 'vertical';
    var cls = classNames((_b = {
            'weda-ui': true,
            'weda-select': true
        },
        _b[className] = className,
        _b));
    var subCls = classNames({
        'weui-cell': true,
        'weui-cell_active': true,
        'weui-cell_form': true,
        'weui-flex__item': isFlex,
        'weui-cell_disabled': disabled,
    });
    var onDateclick = function () {
        var options = {
            className: 'weda-ui weda-picker',
        };
        !!date && (options.defaultDate = date);
        !!startDate && (options.start = decodeIsoDatetime(startDate, 'date'));
        !!endDate && (options.end = decodeIsoDatetime(endDate, 'date'));
        weui.datePicker(__assign(__assign({}, options), { onConfirm: function (result) {
                var value = dayjs()
                    .year(result[0].value)
                    .month(result[1].value - 1)
                    .date(result[2].value)
                    .format('YYYY-MM-DD');
                setDate(value);
                events.change({ value: new Date(value).getTime() });
            } }));
    };
    var onTimeClick = function () {
        var options = {
            className: 'weda-ui weda-picker',
        };
        !!time && (options.defaultTime = time);
        !!startTime && (options.start = decodeIsoDatetime(startTime, 'time'));
        !!endTime && (options.end = decodeIsoDatetime(endTime, 'time'));
        timePicker(__assign(__assign({}, options), { onConfirm: function (result) {
                var date = dayjs().hour(result[0].value).minute(result[1].value);
                var value = date.format('HH:mm');
                setTime(value);
                events.change({
                    value: (date.hour() * 60 * 60 + date.minute() * 60) * 1000,
                });
            } }));
    };
    var onSelect = function () {
        weui.picker(range, {
            defaultValue: [selectedValue || range[0].value],
            onConfirm: function (result) {
                var item = result[0];
                setSelectedValue(item === null || item === void 0 ? void 0 : item.value);
                events.change({ value: item === null || item === void 0 ? void 0 : item.value });
            },
            className: 'weda-ui weda-picker',
        });
    };
    var onRegionClick = function () {
        regionPicker({
            defaultValue: defaultRegion && defaultRegion.length <= 0
                ? ['北京市', '北京市', '东城区']
                : defaultRegion,
            onConfirm: function (result) {
                var value = result.map(function (item) { return item.value; });
                setRegion(value.join(separator));
                events.change({ value: value });
            },
            className: 'weda-ui weda-picker',
        });
    };
    var onClick = function () {
        if (disabled) {
            return;
        }
        switch (mode) {
            case 'date': {
                onDateclick();
                break;
            }
            case 'time': {
                onTimeClick();
                break;
            }
            case 'selector': {
                onSelect();
                break;
            }
            case 'region': {
                onRegionClick();
                break;
            }
            default: {
                break;
            }
        }
    };
    var result = {
        selector: (_c = range.find(function (item) { return item.value === selectedValue; })) === null || _c === void 0 ? void 0 : _c.label,
        date: date,
        time: time,
        region: region,
    };
    return (React.createElement(FormCell, { id: id, className: cls, style: style, label: labelVisible ? label : null, layout: layout, multiCell: false, requiredFlag: requiredFlag },
        React.createElement("div", { className: subCls },
            React.createElement("div", { className: "weui-cell__bd weui-flex" },
                React.createElement("input", { onClick: onClick, className: "weui-input", type: "select", name: name, placeholder: placeholder, value: result[mode], disabled: disabled, autoComplete: 'off', readOnly: true })))));
}
export function decodeIsoDatetime(isoStringOrTimesnap, type) {
    if (!isoStringOrTimesnap) {
        return null;
    }
    if (typeof isoStringOrTimesnap === 'string' &&
        /^[0-9]*$/.test(isoStringOrTimesnap)) {
        var timeSnap = Number(isoStringOrTimesnap);
        if (!isNaN(timeSnap)) {
            isoStringOrTimesnap = timeSnap;
        }
    }
    if (type === 'time') {
        var time = isoStringOrTimesnap.match &&
            isoStringOrTimesnap.match(/^(\d{2}):(\d{2})$/);
        if (time && time[1] < 24 && time[2] < 60) {
            return dayjs().hour(time[1]).minute(time[2]).format('HH:mm');
        }
        else if (typeof isoStringOrTimesnap === 'number') {
            var timeSnap = Number(isoStringOrTimesnap);
            var day = void 0;
            try {
                day = dayjs()
                    .hour(Math.floor(timeSnap / 3600000) % 24)
                    .minute(Math.floor((timeSnap % 3600000) / 60000));
            }
            catch (e) {
                day = dayjs(timeSnap);
            }
            return day.format('HH:mm');
        }
        else {
            return dayjs(isoStringOrTimesnap).format('HH:mm');
        }
    }
    else {
        return dayjs(isoStringOrTimesnap).format('YYYY-MM-DD');
    }
}
//# sourceMappingURL=h5.js.map