// Copyright 2022-2025, University of Colorado Boulder
import '../../../globals.js';
import { LineBreaker } from 'linebreak-ts';
import Range from '../../../dot/js/Range.js';
import optionize from '../../../phet-core/js/optionize.js';
/**
 * Returns an array of ranges that each cover a section of the string where they should not be split by line breaks.
 * These ranges may exclude things like whitespace in-between words, so if a line is being used, the ranges included
 * should just use the starting-min and ending-max to determine what should be included.
 */
const getLineBreakRanges = (str, providedOptions) => {
    const options = optionize()({
        requiredOnly: false
    }, providedOptions);
    const ranges = [];
    const lineBreaker = new LineBreaker(str);
    // Make it iterable (this was refactored out, but the typing was awkward)
    // @ts-expect-error
    lineBreaker[Symbol.iterator] = () => {
        return {
            next() {
                const value = lineBreaker.nextBreak();
                if (value !== null) {
                    return { value: value, done: false };
                }
                else {
                    return { done: true };
                }
            }
        };
    };
    let lastIndex = 0;
    // @ts-expect-error
    for (const brk of lineBreaker) {
        const index = brk.position;
        if (options.requiredOnly && !brk.required) {
            continue;
        }
        // Don't include empty ranges, if they occur.
        if (lastIndex !== index) {
            ranges.push(new Range(lastIndex, index));
        }
        lastIndex = brk.position;
    }
    // Ending range, if it's not empty
    if (lastIndex < str.length) {
        ranges.push(new Range(lastIndex, str.length));
    }
    return ranges;
};
export default getLineBreakRanges;
//# sourceMappingURL=getLineBreakRanges.js.map