"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 });
exports.ExpandableRow = void 0;
const react_1 = __importStar(require("react"));
const aksel_icons_1 = require("@navikt/aksel-icons");
const utils_external_1 = require("../utils-external");
const helpers_1 = require("../utils/helpers");
const hooks_1 = require("../utils/hooks");
const i18n_hooks_1 = require("../utils/i18n/i18n.hooks");
const AnimateHeight_1 = __importDefault(require("./AnimateHeight"));
const DataCell_1 = __importDefault(require("./DataCell"));
const Row_1 = __importDefault(require("./Row"));
const Table_utils_1 = require("./Table.utils");
exports.ExpandableRow = (0, react_1.forwardRef)((_a, ref) => {
    var { className, children, content, togglePlacement = "left", defaultOpen = false, open, onOpenChange, expansionDisabled = false, expandOnRowClick = false, colSpan = 999, contentGutter, onClick } = _a, rest = __rest(_a, ["className", "children", "content", "togglePlacement", "defaultOpen", "open", "onOpenChange", "expansionDisabled", "expandOnRowClick", "colSpan", "contentGutter", "onClick"]);
    const [_open, _setOpen] = (0, hooks_1.useControllableState)({
        defaultValue: defaultOpen,
        value: open,
        onChange: onOpenChange,
    });
    const translate = (0, i18n_hooks_1.useI18n)("global");
    const id = (0, utils_external_1.useId)();
    const expansionHandler = (event) => {
        _setOpen((oldOpen) => !oldOpen);
        event.stopPropagation();
    };
    const handleRowClick = (event) => {
        expandOnRowClick &&
            !expansionDisabled &&
            !(0, Table_utils_1.isElementInteractiveTarget)(event.target) &&
            expansionHandler(event);
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(Row_1.default, Object.assign({}, rest, { ref: ref, className: (0, helpers_1.cl)("aksel-table__expandable-row", className, {
                "aksel-table__expandable-row--open": _open,
                "aksel-table__expandable-row--expansion-disabled": expansionDisabled,
                "aksel-table__expandable-row--clickable": expandOnRowClick,
            }), onClick: (0, helpers_1.composeEventHandlers)(onClick, handleRowClick) }),
            togglePlacement === "right" && children,
            react_1.default.createElement(DataCell_1.default, { className: (0, helpers_1.cl)("aksel-table__toggle-expand-cell", {
                    "aksel-table__toggle-expand-cell--open": _open,
                }), onClick: !expansionDisabled ? expansionHandler : () => null }, !expansionDisabled && (react_1.default.createElement("button", { className: "aksel-table__toggle-expand-button", type: "button", "aria-controls": id, "aria-expanded": _open, onClick: expansionHandler },
                react_1.default.createElement(aksel_icons_1.ChevronDownIcon, { className: "aksel-table__expandable-icon", title: _open ? translate("showLess") : translate("showMore") })))),
            togglePlacement === "left" && children),
        react_1.default.createElement("tr", { "data-state": _open ? "open" : "closed", className: "aksel-table__expanded-row", "aria-hidden": !_open, id: id },
            react_1.default.createElement("td", { colSpan: colSpan, className: "aksel-table__expanded-row-cell" },
                react_1.default.createElement(AnimateHeight_1.default, { className: "aksel-table__expanded-row-collapse", innerClassName: `aksel-table__expanded-row-content aksel-table__expanded-row-content--gutter-${contentGutter !== null && contentGutter !== void 0 ? contentGutter : togglePlacement}`, height: _open ? "auto" : 0, duration: 150, easing: "cubic-bezier(0.39,0.57,0.56,1)" }, content)))));
});
exports.default = exports.ExpandableRow;
//# sourceMappingURL=ExpandableRow.js.map