import { __assign, __rest } from "tslib";
import React, { cloneElement } from "react";
import P2 from "../typography/P2";
import { SwitchComponent, SwitchInput, SwitchSlider, SwitchWrapper, TextAfter, TextBefore, SwitchSliderIndeterminate } from "./style";
var Switch = function (_a) {
    var isChecked = _a.isChecked, isDisabled = _a.isDisabled, className = _a.className, hasTextOrIcon = _a.hasTextOrIcon, onChange = _a.onChange, iconBefore = _a.iconBefore, iconAfter = _a.iconAfter, textAfter = _a.textAfter, textBefore = _a.textBefore, isIndeterminate = _a.isIndeterminate, textDisabled = _a.textDisabled, _b = _a.testId, testId = _b === void 0 ? "switch" : _b, props = __rest(_a, ["isChecked", "isDisabled", "className", "hasTextOrIcon", "onChange", "iconBefore", "iconAfter", "textAfter", "textBefore", "isIndeterminate", "textDisabled", "testId"]);
    var handleChange = function (evt) {
        onChange && onChange(evt, !isChecked);
    };
    var renderBefore = function () {
        if (hasTextOrIcon && !isIndeterminate) {
            if (isChecked) {
                if (iconBefore) {
                    return cloneElement(iconBefore, {
                        className: "beforeSVG",
                        width: 12
                    });
                }
                return (React.createElement(TextBefore, null,
                    React.createElement(P2, { as: "div", type: "cetus" }, textBefore ? textBefore : "Вкл.")));
            }
        }
        return null;
    };
    var renderAfter = function () {
        if (hasTextOrIcon && !isIndeterminate) {
            if (!isChecked) {
                if (iconAfter) {
                    return cloneElement(iconAfter, {
                        className: "afterSVG",
                        width: 12
                    });
                }
                return (React.createElement(TextAfter, null,
                    React.createElement(P2, { as: "div", type: "cetus" }, isDisabled ? textDisabled || "н/д" : textAfter || "Выкл.")));
            }
        }
        return null;
    };
    return (React.createElement(SwitchComponent, __assign({ className: className, "data-testid": testId }, props),
        React.createElement(SwitchWrapper, { isDisabled: isDisabled, isChecked: isChecked, hasTextOrIcon: hasTextOrIcon, iconAfter: iconAfter, isIndeterminate: isIndeterminate, iconBefore: iconBefore, onClick: handleChange },
            renderBefore(),
            React.createElement(SwitchInput, { "data-testid": testId + "-input", "data-element": "switch-input", type: "checkbox", checked: isChecked, name: "checkbox", readOnly: true }),
            isIndeterminate ? (React.createElement(SwitchSliderIndeterminate, null)) : (React.createElement(SwitchSlider, { "data-element": "switch-slider", isChecked: isChecked, isDisabled: isDisabled, hasTextOrIcon: hasTextOrIcon })),
            renderAfter())));
};
export default Switch;
