import forceHiddenStyles from "./forceHiddenStyles";
var hiddenTextarea = null;
var getHeight = function (node, sizingData) {
    var height = node.scrollHeight;
    if (sizingData.sizingStyle.boxSizing === "border-box") {
        // border-box: add border, since height = content + padding + border
        return height + sizingData.borderSize;
    }
    // remove padding, since height = content
    return height - sizingData.paddingSize;
};
export default function calculateNodeHeight(sizingData, value, minRows, maxRows) {
    if (minRows === void 0) { minRows = 1; }
    if (maxRows === void 0) { maxRows = Infinity; }
    if (!hiddenTextarea) {
        hiddenTextarea = document.createElement("textarea");
        hiddenTextarea.setAttribute("tabindex", "-1");
        hiddenTextarea.setAttribute("aria-hidden", "true");
        forceHiddenStyles(hiddenTextarea);
    }
    if (hiddenTextarea.parentNode === null) {
        document.body.appendChild(hiddenTextarea);
    }
    var paddingSize = sizingData.paddingSize, borderSize = sizingData.borderSize, sizingStyle = sizingData.sizingStyle;
    var boxSizing = sizingStyle.boxSizing;
    Object.keys(sizingStyle).forEach(function (_key) {
        var key = _key;
        if (hiddenTextarea) {
            hiddenTextarea.style[key] = sizingStyle[key];
        }
    });
    forceHiddenStyles(hiddenTextarea);
    hiddenTextarea.value = value;
    var height = getHeight(hiddenTextarea, sizingData);
    // measure height of a textarea with a single row
    hiddenTextarea.value = "x";
    var rowHeight = hiddenTextarea.scrollHeight - paddingSize;
    var minHeight = rowHeight * minRows;
    if (boxSizing === "border-box") {
        minHeight = minHeight + paddingSize + borderSize;
    }
    height = Math.max(minHeight, height);
    var maxHeight = rowHeight * maxRows;
    if (boxSizing === "border-box") {
        maxHeight = maxHeight + paddingSize + borderSize;
    }
    height = Math.min(maxHeight, height);
    return [height, rowHeight];
}
