"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadText = exports.loadSegments = void 0;
const constants_js_1 = require("../constants.js");
const mergeTreeNodes_js_1 = require("../mergeTreeNodes.js");
const ops_js_1 = require("../ops.js");
const referencePositions_js_1 = require("../referencePositions.js");
const segmentInfos_js_1 = require("../segmentInfos.js");
const textSegment_js_1 = require("../textSegment.js");
const defaultInsertionInfo = {
    clientId: constants_js_1.NonCollabClient,
    seq: constants_js_1.UniversalSequenceNumber,
};
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 = mergeTreeNodes_js_1.Marker.make(ops_js_1.ReferenceType.Tile, { [referencePositions_js_1.reservedTileLabelsKey]: ["pg"] });
        }
        if (withProps) {
            if (paragraph.includes("Chapter") || paragraph.includes("PRIDE AND PREJ")) {
                if (pgMarker) {
                    pgMarker.properties = { header: 2 };
                    segments.push((0, segmentInfos_js_1.overwriteInfo)(new textSegment_js_1.TextSegment(paragraph), defaultInsertionInfo));
                }
                else {
                    segments.push((0, segmentInfos_js_1.overwriteInfo)(textSegment_js_1.TextSegment.make(paragraph, { fontSize: "140%", lineHeight: "150%" }), defaultInsertionInfo));
                }
            }
            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((0, segmentInfos_js_1.overwriteInfo)(textSegment_js_1.TextSegment.make(emphStrings[i], { fontStyle: "italic" }), defaultInsertionInfo));
                        }
                    }
                    else {
                        if (emphStrings[i].length > 0) {
                            segments.push((0, segmentInfos_js_1.overwriteInfo)(new textSegment_js_1.TextSegment(emphStrings[i]), defaultInsertionInfo));
                        }
                    }
                }
            }
        }
        else {
            segments.push((0, segmentInfos_js_1.overwriteInfo)(new textSegment_js_1.TextSegment(paragraph), defaultInsertionInfo));
        }
        if (pgMarker) {
            segments.push((0, segmentInfos_js_1.overwriteInfo)(pgMarker, defaultInsertionInfo));
        }
    }
    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);
    return mergeTree;
}
exports.loadText = loadText;
//# sourceMappingURL=text.js.map