"use strict";
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 });
exports.Tr = exports.TrBase = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const button_1 = __importDefault(require("../button"));
const core_1 = require("../core");
const utils_1 = require("../utils");
const context_1 = require("./context");
const td_1 = __importDefault(require("./td"));
const trClassName = 'vui-tr';
const buttonClassName = (isExpanded) => `falAngle${isExpanded ? 'Down' : 'Right'}`;
exports.TrBase = core_1.styled.trBox `
  position: relative;
  transition-duration: fast;
`;
/** Displays Table row that can contain heading or data cells. */
exports.Tr = (0, core_1.vui)((props, ref) => {
    var _a;
    const { className, isExpanded: isExpandedProp, isSelected, nested, onCollapsed, onExpanded } = props, rest = __rest(props, ["className", "isExpanded", "isSelected", "nested", "onCollapsed", "onExpanded"]);
    const { variant } = (_a = (0, context_1.useTableContext)()) !== null && _a !== void 0 ? _a : {};
    const styles = (0, core_1.useStyleConfig)('Table', (0, context_1.useTableContext)());
    const [isExpanded, setIsExpanded] = (0, react_1.useState)(isExpandedProp);
    (0, react_1.useEffect)(() => {
        setIsExpanded(isExpandedProp);
    }, [isExpandedProp]);
    const onToggle = () => {
        if (isExpanded) {
            setIsExpanded(false);
            onCollapsed === null || onCollapsed === void 0 ? void 0 : onCollapsed();
        }
        else {
            setIsExpanded(true);
            onExpanded === null || onExpanded === void 0 ? void 0 : onExpanded();
        }
    };
    const selectedProps = isSelected ? { bg: 'skyBlue.selected' } : {};
    rest.children = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [nested !== undefined && ((0, jsx_runtime_1.jsx)(td_1.default, { maxW: 16, px: 0, textAlign: "center", children: nested ? (0, jsx_runtime_1.jsx)(button_1.default, { icon: buttonClassName(isExpanded), onClick: onToggle, variant: "subtleBlue" }) : null })), rest.children] }));
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(exports.TrBase, Object.assign({ className: (0, utils_1.cs)(trClassName, `${trClassName}-${isExpanded ? 'expanded' : 'collapsed'}`, className), "data-selected": Boolean(isSelected), "data-variant": variant, ref: ref }, styles.tr, selectedProps, rest)), isExpanded && nested] }));
});
exports.Tr.displayName = 'Tr';
exports.default = exports.Tr;
//# sourceMappingURL=tr.js.map