<commit_before>import {Assert, Clone, E} from "js-vextensions";
import keycode from "keycode";
import {forwardRef, Ref} from "react";
import {Button, Pre, Row, TextArea} from "react-vcomponents";
import {FilterOutUnrecognizedProps, UseImperativeHandle, UseState, Wrap, BaseComponentPlus, WarnOfTransientObjectProps} from "react-vextensions";
import {AddNodeRevision} from "Server/Commands/AddNodeRevision";
import {Map} from "Store/firebase/maps/@Map";
import {GetParentNode, IsNodeSubnode} from "Store/firebase/nodes";
import {GetFontSizeForNode, GetNodeDisplayText, GetNodeForm, missingTitleStrings} from "Store/firebase/nodes/$node";
import {GetEquationStepNumber} from "Store/firebase/nodes/$node/equation";
import {ClaimForm, MapNodeL2} from "Store/firebase/nodes/@MapNode";
import {MapNodeRevision_titlePattern} from "Store/firebase/nodes/@MapNodeRevision";
import {MapNodeType} from "Store/firebase/nodes/@MapNodeType";
import {MeID} from "Store/firebase/users";
import {InfoButton, IsDoubleClick, ParseSegmentsForPatterns, VReactMarkdown_Remarkable, Observer} from "vwebapp-framework";
import {ES} from "Utils/UI/GlobalStyles";
import {store} from "Store";
import {MapNodeView, GetNodeViewsAlongPath, GetNodeView} from "Store/main/maps/mapViews/$mapView";
import {runInAction} from "mobx";
import {CanEditNode} from "Store/firebase/users/$user";
import {GetTermsAttached} from "Store/firebase/terms";
import _ from "lodash";
import {Term} from "Store/firebase/terms/@Term";
import {NodeMathUI} from "../NodeMathUI";
import {NodeUI_Inner} from "../NodeUI_Inner";
import {TermPlaceholder} from "./TermPlaceholder";

/* type TitlePanelProps = {parent: NodeUI_Inner, map: Map, node: MapNodeL2, nodeView: MapNodeView, path: string, indexInNodeList: number, style};
const TitlePanel_connector = (state, { node, path }: TitlePanelProps) => ({
	displayText: GetNodeDisplayText(node, path),
	$1: node.current.image && GetImage(node.current.image.id),
	equationNumber: node.current.equation ? GetEquationStepNumber(path) : null,
});
@Connect(TitlePanel_connector)
// export class TitlePanel extends BaseComponentWithConnector(TitlePanel_connector, { editing: false, newTitle: null as string, applyingEdit: false }) { */

/* export type TitlePanelInternals = {OnDoubleClick};
export function TitlePanel(props: VProps<TitlePanelInternals, {
	parent: NodeUI_Inner, map: Map, node: MapNodeL2, nodeView: MapNodeView, path: string, indexInNodeList: number, style,
}>) { */

export function GetSegmentsForTerms(text: string, termsToSearchFor: Term[]) {
	// let segments = ParseSegmentsFromNodeDisplayText(text);
	/*const segments = ParseSegmentsForPatterns(text, [
		{name: "term", regex: /{(.+?)\}\[(.+?)\]/},
	]);*/
	/*const patterns = termsToSearchFor.SelectMany(term=>{
		return term.forms.map(form=>{
			return {name: `termForm`, termID: term._key, regex: new RegExp(`(^|\\W)(${_.escapeRegExp(form)})(\\W|$)`)};
		});
	});
	return ParseSegmentsForPatterns(text, patterns);*/

	//const termForm_termIDs = termsToSearchFor.SelectMany(term=>term.forms.map(a=>term._key));
	let patterns = [];
	if (termsToSearchFor.length) {
		const termForm_strings = termsToSearchFor.SelectMany(term=>term.forms.map(form=>_.escapeRegExp(form)));
		const regex = new RegExp(`(^|\\W)(${termForm_strings.join("|")})(\\W|$)`, "i");
		//const patterns = [{name: "termForm", termForm_termIDs, regex}];
		patterns = [{name: "termForm", regex}];
	}<commit_msg>* MS GetSegmentsForTerms prefers matching long-forms over short-forms.<commit_after>import {Assert, Clone, E} from "js-vextensions";
import keycode from "keycode";
import {forwardRef, Ref} from "react";
import {Button, Pre, Row, TextArea} from "react-vcomponents";
import {FilterOutUnrecognizedProps, UseImperativeHandle, UseState, Wrap, BaseComponentPlus, WarnOfTransientObjectProps} from "react-vextensions";
import {AddNodeRevision} from "Server/Commands/AddNodeRevision";
import {Map} from "Store/firebase/maps/@Map";
import {GetParentNode, IsNodeSubnode} from "Store/firebase/nodes";
import {GetFontSizeForNode, GetNodeDisplayText, GetNodeForm, missingTitleStrings} from "Store/firebase/nodes/$node";
import {GetEquationStepNumber} from "Store/firebase/nodes/$node/equation";
import {ClaimForm, MapNodeL2} from "Store/firebase/nodes/@MapNode";
import {MapNodeRevision_titlePattern} from "Store/firebase/nodes/@MapNodeRevision";
import {MapNodeType} from "Store/firebase/nodes/@MapNodeType";
import {MeID} from "Store/firebase/users";
import {InfoButton, IsDoubleClick, ParseSegmentsForPatterns, VReactMarkdown_Remarkable, Observer} from "vwebapp-framework";
import {ES} from "Utils/UI/GlobalStyles";
import {store} from "Store";
import {MapNodeView, GetNodeViewsAlongPath, GetNodeView} from "Store/main/maps/mapViews/$mapView";
import {runInAction} from "mobx";
import {CanEditNode} from "Store/firebase/users/$user";
import {GetTermsAttached} from "Store/firebase/terms";
import _ from "lodash";
import {Term} from "Store/firebase/terms/@Term";
import {NodeMathUI} from "../NodeMathUI";
import {NodeUI_Inner} from "../NodeUI_Inner";
import {TermPlaceholder} from "./TermPlaceholder";

/* type TitlePanelProps = {parent: NodeUI_Inner, map: Map, node: MapNodeL2, nodeView: MapNodeView, path: string, indexInNodeList: number, style};
const TitlePanel_connector = (state, { node, path }: TitlePanelProps) => ({
	displayText: GetNodeDisplayText(node, path),
	$1: node.current.image && GetImage(node.current.image.id),
	equationNumber: node.current.equation ? GetEquationStepNumber(path) : null,
});
@Connect(TitlePanel_connector)
// export class TitlePanel extends BaseComponentWithConnector(TitlePanel_connector, { editing: false, newTitle: null as string, applyingEdit: false }) { */

/* export type TitlePanelInternals = {OnDoubleClick};
export function TitlePanel(props: VProps<TitlePanelInternals, {
	parent: NodeUI_Inner, map: Map, node: MapNodeL2, nodeView: MapNodeView, path: string, indexInNodeList: number, style,
}>) { */

export function GetSegmentsForTerms(text: string, termsToSearchFor: Term[]) {
	// let segments = ParseSegmentsFromNodeDisplayText(text);
	/*const segments = ParseSegmentsForPatterns(text, [
		{name: "term", regex: /{(.+?)\}\[(.+?)\]/},
	]);*/
	/*const patterns = termsToSearchFor.SelectMany(term=>{
		return term.forms.map(form=>{
			return {name: `termForm`, termID: term._key, regex: new RegExp(`(^|\\W)(${_.escapeRegExp(form)})(\\W|$)`)};
		});
	});
	return ParseSegmentsForPatterns(text, patterns);*/

	//const termForm_termIDs = termsToSearchFor.SelectMany(term=>term.forms.map(a=>term._key));
	let patterns = [];
	if (termsToSearchFor.length) {
		const termForm_strings = termsToSearchFor.SelectMany(term=>{
			const formsForTerm = term.forms.map(form=>_.escapeRegExp(form));
			return formsForTerm.OrderByDescending(a=>a.length); // prefer matching long-forms over short-forms
		});
		const regex = new RegExp(`(^|\\W)(${termForm_strings.join("|")})(\\W|$)`, "i");
		//const patterns = [{name: "termForm", termForm_termIDs, regex}];
		patterns = [{name: "termForm", regex}];
	}
{'fraction_non_alphanumeric': 0.15195800227014755, 'fraction_numerical': 0.0011350737797956867, 'mean_word_length': 6.9024663677130045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1789049', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2226, 'n_words': 511}