"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentDiskRoleLabel = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const react_core_1 = require("@patternfly/react-core");
const config_1 = require("../../config");
const hooks_1 = require("../../hooks");
const use_translation_wrapper_1 = require("../../hooks/use-translation-wrapper");
const getCurrentDiskRoleLabel = (disk, installationDiskId, t) => (disk.id === installationDiskId ? (0, config_1.diskRoleLabels)(t).install : (0, config_1.diskRoleLabels)(t).none);
exports.getCurrentDiskRoleLabel = getCurrentDiskRoleLabel;
const DiskRole = ({ host, disk, installationDiskId, isEditable, onDiskRole, }) => {
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const currentRoleLabel = (0, exports.getCurrentDiskRoleLabel)(disk, installationDiskId, t);
    if (isEditable && disk.id !== installationDiskId && onDiskRole) {
        return (react_1.default.createElement(DiskRoleDropdown, { host: host, disk: disk, installationDiskId: installationDiskId, onDiskRole: onDiskRole }));
    }
    return react_1.default.createElement(react_1.default.Fragment, null, currentRoleLabel);
};
const DiskRoleDropdown = ({ host, disk, installationDiskId, onDiskRole, }) => {
    var _a, _b;
    const [isOpen, setOpen] = (0, hooks_1.useStateSafely)(false);
    const [isDisabled, setDisabled] = (0, hooks_1.useStateSafely)(false);
    const { t } = (0, use_translation_wrapper_1.useTranslation)();
    const dropdownItems = [
        react_1.default.createElement(react_core_1.DropdownItem, { key: "install", id: "install", isDisabled: !((_a = disk.installationEligibility) === null || _a === void 0 ? void 0 : _a.eligible), description: !((_b = disk.installationEligibility) === null || _b === void 0 ? void 0 : _b.eligible) && t('ai:Disk is not eligible for installation') }, (0, config_1.diskRoleLabels)(t).install),
    ];
    const onSelect = react_1.default.useCallback((event) => {
        const asyncFunc = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            if (event === null || event === void 0 ? void 0 : event.currentTarget.id) {
                setDisabled(true);
                yield onDiskRole(host.id, disk.id, event.currentTarget.id);
                setDisabled(false);
            }
            setOpen(false);
        });
        void asyncFunc();
    }, [setOpen, setDisabled, onDiskRole, host.id, disk.id]);
    const currentRoleLabel = (0, exports.getCurrentDiskRoleLabel)(disk, installationDiskId, t);
    const toggle = (toggleRef) => (react_1.default.createElement(react_core_1.MenuToggle, { variant: "plainText", ref: toggleRef, isFullWidth: true, onClick: () => setOpen(!isOpen), isExpanded: isOpen, isDisabled: isDisabled, "data-testid": "disk-role-dropdown-toggle" }, currentRoleLabel));
    return (react_1.default.createElement(react_core_1.Dropdown, { onOpenChange: () => setOpen(!isOpen), onSelect: onSelect, toggle: toggle, isOpen: isOpen },
        react_1.default.createElement(react_core_1.DropdownList, null, dropdownItems)));
};
exports.default = DiskRole;
//# sourceMappingURL=DiskRole.js.map