"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Textbox = void 0;
var tslib_1 = require("tslib");
var React = require("react");
var d3_selection_1 = require("d3-selection");
var utilities_1 = require("../../../utilities/utilities");
var Textbox = function (props) {
    var textElementRef = React.useRef(null);
    var wrapWords = function () {
        if (!textElementRef.current) {
            return;
        }
        var text = (0, d3_selection_1.select)(textElementRef.current);
        var words = props.text.split(/\s+/);
        var line = [];
        var tspan = text.append('tspan');
        var numLines = 0;
        words.forEach(function (word) {
            line.push(word);
            tspan.text(line.join(' '));
            var node = tspan.node();
            if (node && node.getComputedTextLength() > props.width && line.length > 1) {
                numLines++;
                line.pop();
                tspan.text(line.join(' '));
                line = [word];
                tspan = text.append('tspan').text(word).attr('dy', props.lineHeight).attr('x', props.x);
            }
        });
        // bottom aligns text
        text.attr('dy', -numLines * props.lineHeight);
        return function () {
            text.selectAll('tspan').remove();
        };
    };
    React.useEffect(wrapWords);
    var lineHeight = props.lineHeight, rest = tslib_1.__rest(props, ["lineHeight"]);
    return React.createElement("text", tslib_1.__assign({ ref: textElementRef }, (0, utilities_1.getSecureProps)(rest)));
};
exports.Textbox = Textbox;
//# sourceMappingURL=Textbox.js.map