"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const Icon_1 = __importStar(require("../Icon/Icon"));
const Overlay_1 = require("../Overlay/Overlay");
const RadioButton_1 = require("./RadioButton");
const Tooltip_1 = __importDefault(require("../Tooltip/Tooltip"));
const RadioButtonGroup_module_scss_1 = __importDefault(require("./RadioButtonGroup.module.scss"));
const Option = ({ option: optionProp, value: valueProp, onChange, renderChildrenContent, isSubOption, }) => {
    const options = optionProp.options;
    const isGroup = !!(options === null || options === void 0 ? void 0 : options.length);
    const [ref, setRef] = (0, react_1.useState)(null);
    const [open, setOpen] = (0, react_1.useState)(false);
    const option = isGroup && valueProp ? options.find(({ value }) => value === valueProp) || optionProp : optionProp;
    const radioButton = (react_1.default.createElement(RadioButton_1.RadioButton, { headerRef: setRef, className: RadioButtonGroup_module_scss_1.default.item, key: option === null || option === void 0 ? void 0 : option.value, checked: !(option === null || option === void 0 ? void 0 : option.disabled) && (option === null || option === void 0 ? void 0 : option.value) === valueProp, onChange: () => {
            if (option === null || option === void 0 ? void 0 : option.disabled)
                return;
            if (isGroup)
                setOpen(!open);
            else if ((option === null || option === void 0 ? void 0 : option.value) && (option === null || option === void 0 ? void 0 : option.value) !== valueProp)
                onChange === null || onChange === void 0 ? void 0 : onChange(option.value, optionProp.value);
        }, label: option === null || option === void 0 ? void 0 : option.label, disabled: option === null || option === void 0 ? void 0 : option.disabled, renderContent: option === null || option === void 0 ? void 0 : option.renderContent }));
    const isSelected = !isGroup && (option === null || option === void 0 ? void 0 : option.value) === valueProp;
    const isExpanded = isGroup && open;
    const dropDownIndicatorStyle = !(option === null || option === void 0 ? void 0 : option.renderContent) && !isGroup
        ? { transform: 'translateX(1px) translateY(-2px) rotate(270deg)' }
        : isSelected || isExpanded
            ? { transform: 'scaleY(-1) translateY(2px)' }
            : undefined;
    if ((option === null || option === void 0 ? void 0 : option.disabled) && (option === null || option === void 0 ? void 0 : option.disabledReason)) {
        return (react_1.default.createElement(Tooltip_1.default, { key: option === null || option === void 0 ? void 0 : option.value, wrapperClasses: RadioButtonGroup_module_scss_1.default.wrapper, title: option === null || option === void 0 ? void 0 : option.disabledReason }, radioButton));
    }
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { key: option === null || option === void 0 ? void 0 : option.value, className: RadioButtonGroup_module_scss_1.default.wrapper },
            radioButton,
            !isSubOption && (react_1.default.createElement("div", { className: RadioButtonGroup_module_scss_1.default.childrenIndicator, style: dropDownIndicatorStyle },
                react_1.default.createElement(Icon_1.default, { size: "24px", icon: Icon_1.IconType.Dropdown })))),
        options && (react_1.default.createElement(Overlay_1.Overlay, { onClose: () => setOpen(false), className: RadioButtonGroup_module_scss_1.default.overlay, hideStyles: true, containerClassName: "u-w-100", open: open, followElement: ref || undefined, offset: [0, 0], placement: "bottom-start" },
            react_1.default.createElement(RadioButtonGroup, { options: options.map((option) => {
                    const { renderContent } = option, rest = __rest(option, ["renderContent"]);
                    if (!renderChildrenContent)
                        return rest;
                    return option;
                }), onChange: (subValue) => {
                    onChange === null || onChange === void 0 ? void 0 : onChange(optionProp.value, subValue);
                    setOpen(false);
                }, value: valueProp, isSubGroup: true })))));
};
const RadioButtonGroup = ({ options = [], value: valueProp, onChange, renderChildrenContent, isSubGroup, }) => {
    return (react_1.default.createElement("div", { className: RadioButtonGroup_module_scss_1.default.root }, options.map((rawOption) => (react_1.default.createElement(Option, { key: rawOption.value, option: rawOption, value: valueProp, onChange: onChange, renderChildrenContent: renderChildrenContent, isSubOption: isSubGroup })))));
};
exports.default = RadioButtonGroup;
//# sourceMappingURL=RadioButtonGroup.js.map