<commit_before>import { ApplicationState, MAX_RECENT_FILE_NAME_COUNT } from '../../../shared/models/applicationState';
import { Editor } from '../../../shared/models/editor';
import { Layer } from '../../../shared/models/layer';
import { Point } from '../../../shared/models/point';
import { PolygenDocument } from '../../../shared/models/polygenDocument';
import { moveElement } from '../../../shared/utils/arrays';
import { makeMostRecent } from '../../../shared/utils/recentItemList';
import { defineAction } from '../../reduxWithLessSux/action';

interface OpenExistingProjectFilePayload {
	fileName: string;
	document: PolygenDocument;
}

function indexOfEditorByFileName(editors: Editor[], fileName: string): number {
	const editorIndex = editors.reduce((result, editor, index) => {
		if (result === -1 && editor.fileName === fileName) {
			result = index;
		}
		return result;
	}, -1);

	return editorIndex;
}

export const openExistingProjectFile = defineAction(
	'openExistingProjectFile',
	(state: ApplicationState, payload: OpenExistingProjectFilePayload) => {
		let editorIndex = indexOfEditorByFileName(state.editors, payload.fileName);

		let editors: Editor[];

		if (editorIndex !== -1) {
			editors = state.editors;
		} else {
			const editor: Editor = {
				document: payload.document,
				fileName: payload.fileName,
				hasUnsavedChanges: false,
				mode: 'edit',
				selectedLayerIndex: 0,
				selectedPointIndices: [],<commit_msg>Select top-most layer when opening a document<commit_after>import { ApplicationState, MAX_RECENT_FILE_NAME_COUNT } from '../../../shared/models/applicationState';
import { Editor } from '../../../shared/models/editor';
import { Layer } from '../../../shared/models/layer';
import { Point } from '../../../shared/models/point';
import { PolygenDocument } from '../../../shared/models/polygenDocument';
import { moveElement } from '../../../shared/utils/arrays';
import { makeMostRecent } from '../../../shared/utils/recentItemList';
import { defineAction } from '../../reduxWithLessSux/action';

interface OpenExistingProjectFilePayload {
	fileName: string;
	document: PolygenDocument;
}

function indexOfEditorByFileName(editors: Editor[], fileName: string): number {
	const editorIndex = editors.reduce((result, editor, index) => {
		if (result === -1 && editor.fileName === fileName) {
			result = index;
		}
		return result;
	}, -1);

	return editorIndex;
}

export const openExistingProjectFile = defineAction(
	'openExistingProjectFile',
	(state: ApplicationState, payload: OpenExistingProjectFilePayload) => {
		let editorIndex = indexOfEditorByFileName(state.editors, payload.fileName);

		let editors: Editor[];

		if (editorIndex !== -1) {
			editors = state.editors;
		} else {
			const editor: Editor = {
				document: payload.document,
				fileName: payload.fileName,
				hasUnsavedChanges: false,
				mode: 'edit',
				selectedLayerIndex: payload.document.layers.length - 1,
				selectedPointIndices: [],
{'fraction_non_alphanumeric': 0.15931289996631862, 'fraction_numerical': 0.002694509936005389, 'mean_word_length': 3.95, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2360990', 'n_tokens_mistral': 903, 'n_tokens_neox': 856, 'n_words': 203}