"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadText = exports.loadSegments = void 0;
const MergeTree = __importStar(require("./mergeTree"));
const ops = __importStar(require("./ops"));
const textSegment_1 = require("./textSegment");
function loadSegments(content, segLimit, markers = false, withProps = true) {
    const BOMFreeContent = content.replace(/^\uFEFF/, "");
    const paragraphs = BOMFreeContent.split(/\r?\n/);
    for (let i = 0, len = paragraphs.length; i < len; i++) {
        paragraphs[i] = paragraphs[i]
            .replace(/\r?\n/g, " ")
            .replace(/\u201c|\u201d/g, '"')
            .replace(/\u2019/g, "'");
        if (!markers && i !== paragraphs.length - 1) {
            paragraphs[i] += "\n";
        }
    }
    const segments = [];
    for (const paragraph of paragraphs) {
        let pgMarker;
        if (markers) {
            pgMarker = MergeTree.Marker.make(ops.ReferenceType.Tile, { [MergeTree.reservedTileLabelsKey]: ["pg"] });
        }
        if (withProps) {
            if ((paragraph.includes("Chapter")) || (paragraph.includes("PRIDE AND PREJ"))) {
                if (pgMarker) {
                    pgMarker.addProperties({ header: 2 });
                    segments.push(new textSegment_1.TextSegment(paragraph));
                }
                else {
                    segments.push(textSegment_1.TextSegment.make(paragraph, { fontSize: "140%", lineHeight: "150%" }));
                }
            }
            else {
                const emphStrings = paragraph.split("_");
                for (let i = 0, len = emphStrings.length; i < len; i++) {
                    // eslint-disable-next-line no-bitwise
                    if (i & 1) {
                        if (emphStrings[i].length > 0) {
                            segments.push(textSegment_1.TextSegment.make(emphStrings[i], { fontStyle: "italic" }));
                        }
                    }
                    else {
                        if (emphStrings[i].length > 0) {
                            segments.push(new textSegment_1.TextSegment(emphStrings[i]));
                        }
                    }
                }
            }
        }
        else {
            segments.push(new textSegment_1.TextSegment(paragraph));
        }
        if (pgMarker) {
            segments.push(pgMarker);
        }
    }
    if (segLimit > 0) {
        segments.length = segLimit;
    }
    return segments;
}
exports.loadSegments = loadSegments;
function loadText(content, mergeTree, segLimit, markers = false) {
    const segments = loadSegments(content, segLimit, markers);
    mergeTree.reloadFromSegments(segments);
    // console.log(`Number of Segments: ${segments.length}`);
    // console.log(`Height: ${mergeTree.getStats().maxHeight}`);
    // console.log(segTree.toString());
    return mergeTree;
}
exports.loadText = loadText;
//# sourceMappingURL=text.js.map