"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const formik_1 = require("formik");
const react_core_1 = require("@patternfly/react-core");
const utils_1 = require("./utils");
const exclamation_circle_icon_1 = tslib_1.__importDefault(require("@patternfly/react-icons/dist/js/icons/exclamation-circle-icon"));
const SwitchField = (_a) => {
    var { label, helperText, isRequired, onChange, onChangeCustomOverride, getHelperText, idPostfix, labelIcon, tooltipProps, switchOuiaId } = _a, props = tslib_1.__rest(_a, ["label", "helperText", "isRequired", "onChange", "onChangeCustomOverride", "getHelperText", "idPostfix", "labelIcon", "tooltipProps", "switchOuiaId"]);
    const [field, { touched, error }] = (0, formik_1.useField)(props.name);
    const fieldId = (0, utils_1.getFieldId)(props.name, 'input', idPostfix);
    const isValid = !(touched && error);
    const errorMessage = !isValid ? error : '';
    const hText = getHelperText ? getHelperText(field.value) : helperText;
    const switchFields = Object.assign(Object.assign({}, field), { id: fieldId, label: label, isDisabled: props.isDisabled, isChecked: Boolean(field.value), hasCheckIcon: Boolean(field.value), ouiaId: switchOuiaId, onChange: (event, checked) => {
            if (onChangeCustomOverride) {
                onChangeCustomOverride(checked, event);
            }
            else {
                field.onChange(event);
                onChange && onChange(checked, event);
            }
        } });
    return (React.createElement(react_core_1.FormGroup, { fieldId: fieldId, isRequired: isRequired, labelHelp: labelIcon },
        tooltipProps ? (React.createElement(react_core_1.Tooltip, Object.assign({}, tooltipProps),
            React.createElement(react_core_1.Switch, Object.assign({}, switchFields)))) : (React.createElement(react_core_1.Switch, Object.assign({}, switchFields))),
        (errorMessage || hText) && (React.createElement(react_core_1.FormHelperText, null,
            React.createElement(react_core_1.HelperText, null,
                React.createElement(react_core_1.HelperTextItem, { icon: React.createElement(exclamation_circle_icon_1.default, null), variant: errorMessage ? 'error' : 'default', id: errorMessage ? `${fieldId}-helper-error` : `${fieldId}-helper`, "data-testid": `input-switchfield-${fieldId}-helper-text` }, errorMessage ? errorMessage : hText))))));
};
exports.default = SwitchField;
//# sourceMappingURL=SwitchField.js.map