"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = (0, tslib_1.__importStar)(require("react"));
var classnames_1 = (0, tslib_1.__importDefault)(require("../../../utils/classnames"));
var getLocalCounter_1 = (0, tslib_1.__importDefault)(require("../../../utils/getLocalCounter"));
var formcell_1 = (0, tslib_1.__importDefault)(require("../formcell"));
var platform_1 = require("../../../utils/platform");
var tea_component_1 = require("tea-component");
function Switch(_a) {
    var _b;
    var 
    // 系统属性
    id = _a.id, className = _a.className, style = _a.style, events = _a.events, 
    // 组件属性
    label = _a.label, labelVisible = _a.labelVisible, defaultChecked = _a.checked, layout = _a.layout, disabled = _a.disabled, requiredFlag = _a.requiredFlag;
    var isFlex = layout !== 'vertical';
    var cls = (0, classnames_1.default)((_b = {
            'weda-ui': true,
            'weda-switch': true
        },
        _b[className] = className,
        _b));
    var subCls = (0, classnames_1.default)({
        'weui-cell': true,
        'weui-cell_active': true,
        'weui-cell_switch': true,
        'weui-flex__item': isFlex,
        'weui-cell_disabled': disabled,
    });
    var platform = (0, platform_1.usePlatform)();
    var _c = (0, tslib_1.__read)(React.useState(defaultChecked), 2), checked = _c[0], setChecked = _c[1];
    // 两次默认值不同时, 需要刷新
    var prevDefaultCheckedRef = React.useRef();
    React.useEffect(function () {
        if (prevDefaultCheckedRef.current !== defaultChecked) {
            prevDefaultCheckedRef.current = defaultChecked;
            setChecked(defaultChecked);
        }
    });
    var onChange = function (e) {
        setChecked(e.target.checked);
        events.change({ value: e.target.checked }, { originEvent: e });
    };
    // 现在低码组件往源码组件传入的id不唯一, 暂时使用hashcode替代
    var hashcode = (0, getLocalCounter_1.default)();
    var _id = "switch-" + hashcode;
    var switchElement = platform === 'h5' ? (React.createElement("div", { className: subCls },
        isFlex ? React.createElement("div", { className: "weui-cell__bd" }) : null,
        React.createElement("div", { className: isFlex ? 'weui-cell__ft' : 'weui-cell__bd' },
            React.createElement("label", { htmlFor: _id, key: _id, className: "weui-switch-cp" },
                React.createElement("input", { id: _id, className: "weui-switch-cp__input", type: "checkbox", checked: checked, disabled: disabled, onChange: onChange }),
                React.createElement("div", { className: "weui-switch-cp__box" }))))) : (React.createElement(tea_component_1.Switch, { id: _id, disabled: disabled, onChange: onChange }));
    return (React.createElement(formcell_1.default, { id: id, className: cls, style: style, label: labelVisible ? label : null, layout: layout, multiCell: true, requiredFlag: requiredFlag }, switchElement));
}
exports.default = Switch;
//# sourceMappingURL=index.js.map