"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIHighlightMenuOption = void 0;
const react_1 = __importDefault(require("react"));
require("./UIHighlightMenuOption.scss");
/**
 * UIHighlightMenuOption component.
 *
 * @exports
 * @class UIHighlightMenuOption
 * @extends {React.Component<UIHighlightMenuOptionProps>}
 */
class UIHighlightMenuOption extends react_1.default.Component {
    /**
     * @returns {JSX.Element}
     */
    render() {
        let text = this.props.text;
        let query = this.props.query;
        const parts = [];
        if (text && query) {
            // There is search query - highlight occureance
            text = text.toString();
            query = query.toLowerCase();
            const simpleText = text.toLowerCase();
            const chars = query.length;
            let startIndex = 0;
            let index = simpleText.indexOf(query);
            const fnAddPart = (part) => (part ? parts.push(part) : null);
            // Highlight all occureances
            while (index !== -1) {
                fnAddPart(react_1.default.createElement("span", { key: 'text-' + startIndex }, text.substring(startIndex, index)));
                fnAddPart(react_1.default.createElement("span", { className: "ts-Menu-option--highlighted", key: 'highlight-' + index }, text.substring(index, index + chars)));
                startIndex = index + chars;
                index = simpleText.indexOf(query, index + chars);
                if (index === -1) {
                    fnAddPart(react_1.default.createElement("span", { key: 'text-' + startIndex }, text.substring(startIndex, simpleText.length)));
                }
            }
        }
        if (parts.length === 0) {
            parts.push(text);
        }
        return react_1.default.createElement("div", { className: "ts-Menu-option" }, parts);
    }
}
exports.UIHighlightMenuOption = UIHighlightMenuOption;
//# sourceMappingURL=UIHighlightMenuOption.js.map