"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var react_1 = (0, tslib_1.__importDefault)(require("react"));
var platform_1 = require("../../../utils/platform");
var formcell_1 = (0, tslib_1.__importDefault)(require("../formcell"));
var h5_1 = (0, tslib_1.__importStar)(require("./h5"));
var classnames_1 = (0, tslib_1.__importDefault)(require("../../../utils/classnames"));
var tea_component_1 = require("tea-component");
var moment_1 = (0, tslib_1.__importDefault)(require("moment"));
var isObjectEqual_1 = (0, tslib_1.__importDefault)(require("../../../utils/isObjectEqual"));
var region_1 = require("./region");
function Select(props) {
    var platform = (0, platform_1.usePlatform)();
    if (platform === 'h5') {
        return react_1.default.createElement(h5_1.default, (0, tslib_1.__assign)({}, props));
    }
    return (react_1.default.createElement(tea_component_1.ConfigProvider, { classPrefix: "wedatea2td" },
        react_1.default.createElement(SelectPc, (0, tslib_1.__assign)({}, props))));
}
exports.default = Select;
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 = (0, classnames_1.default)((_b = {
            'weda-ui': true,
            'weda-select': true
        },
        _b[className] = className,
        _b));
    // 两次默认值不同, 需要刷新
    var prevDefaultRef = react_1.default.useRef();
    var _c = (0, tslib_1.__read)(react_1.default.useState((0, h5_1.decodeIsoDatetime)(defaultDate, 'date')), 2), date = _c[0], setDate = _c[1];
    var _d = (0, tslib_1.__read)(react_1.default.useState((0, h5_1.decodeIsoDatetime)(defaultTime, 'time')), 2), time = _d[0], setTime = _d[1];
    var _e = (0, tslib_1.__read)(react_1.default.useState(defaultRegion || undefined), 2), region = _e[0], setRegion = _e[1];
    var _f = (0, tslib_1.__read)(react_1.default.useState(defaultValue), 2), selectedValue = _f[0], setSelectedValue = _f[1];
    react_1.default.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((0, h5_1.decodeIsoDatetime)(defaultDate, 'date'));
            hasChange = true;
        }
        if (((_c = prevDefaultRef.current) === null || _c === void 0 ? void 0 : _c.defaultTime) !== defaultTime) {
            var ts = (0, h5_1.decodeIsoDatetime)(defaultTime, 'time');
            setTime(ts);
            hasChange = true;
        }
        if (!(0, isObjectEqual_1.default)((_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_1.default.createElement(tea_component_1.Select, { 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 = (0, moment_1.default)();
                var start = startDate
                    ? (0, h5_1.decodeIsoDatetime)(startDate, 'date')
                    : now.year() - 20 + "-01-01";
                var end = endDate
                    ? (0, h5_1.decodeIsoDatetime)(endDate, 'date')
                    : now.year() + 20 + "-12-31";
                return (react_1.default.createElement(tea_component_1.DatePicker, { range: [(0, moment_1.default)(start), (0, moment_1.default)(end)], onChange: onDataChange, disabled: disabled, placeholder: placeholder, value: date ? (0, moment_1.default)(date) : undefined }));
            }
            case 'time': {
                var start = startTime
                    ? (0, h5_1.decodeIsoDatetime)(startTime, 'time')
                    : '00:00';
                var end = endTime ? (0, h5_1.decodeIsoDatetime)(endTime, 'time') : '23:59';
                var now = (0, moment_1.default)();
                return (react_1.default.createElement(tea_component_1.TimePicker, { onChange: onDataChange, disabled: disabled, placeholder: placeholder, value: time ? (0, moment_1.default)(time, 'HH:mm') : undefined, range: [(0, moment_1.default)(start, 'HH:mm'), (0, moment_1.default)(end, 'HH:mm')], format: "HH:mm" }));
            }
            case 'region':
                return (react_1.default.createElement(tea_component_1.Cascader, { clearable: true, searchable: true, type: "menu", disabled: disabled, data: (0, region_1.getRegionTree)(), multiple: false, onChange: onDataChange, valueRender: function (options) {
                        return options.map(function (_a, index) {
                            var label = _a.label, value = _a.value;
                            return (react_1.default.createElement(react_1.default.Fragment, { key: value + "-" + index },
                                index !== 0 && "" + separator,
                                label || value));
                        });
                    }, value: region }));
        }
    };
    var dropdown = createDropdown();
    return (react_1.default.createElement(formcell_1.default, { size: size, id: id, className: cls, style: style, label: labelVisible ? label : null, layout: layout, multiCell: false, requiredFlag: requiredFlag }, dropdown));
}
//# sourceMappingURL=index.js.map