<commit_before>import {GetAsync, GetAsync_Raw} from "Frame/Database/DatabaseHelpers";
import {GetNodeL2} from "Store/firebase/nodes/$node";
import {MapNodeRevision} from "Store/firebase/nodes/@MapNodeRevision";
import {Assert, ToInt} from "js-vextensions";
import {GetDataAsync} from "../../Frame/Database/DatabaseHelpers";
import {GetMaps} from "../../Store/firebase/maps";
import {GetNodeRevisions} from "../../Store/firebase/nodeRevisions";
import {GetNodeViewers} from "../../Store/firebase/nodeViewers";
import {ForDelete_GetError} from "../../Store/firebase/nodes";
import {MapNodeL2} from "../../Store/firebase/nodes/@MapNode";
import {Command, MergeDBUpdates} from "../Command";
import {MapEdit, UserEdit} from "../CommandMacros";

@MapEdit
@UserEdit
export default class DeleteNode extends Command<{mapID?: number, nodeID: number, withContainerArgument?: number, asPartOfMapDelete?: boolean}> {
	sub_deleteContainerArgument: DeleteNode;
	
	oldData: MapNodeL2;
	oldRevisions: MapNodeRevision[];
	oldParentChildrenOrders: number[][];
	viewerIDs_main: string[];
	mapIDs: number[];
	async Prepare() {
		let {mapID, nodeID, withContainerArgument, asPartOfMapDelete} = this.payload;
		this.oldData = await GetAsync_Raw(()=>GetNodeL2(nodeID));
		this.oldRevisions = await GetAsync(()=>GetNodeRevisions(nodeID));

		this.oldParentChildrenOrders = await Promise.all((this.oldData.parents || {}).VKeys().map(parentID=> {
			return GetDataAsync("nodes", parentID, "childrenOrder") as Promise<number[]>;
		}));

		this.viewerIDs_main = await GetAsync(()=>GetNodeViewers(nodeID));

		this.mapIDs = await GetAsync(()=>GetMaps().map(a=>a._id));

		if (withContainerArgument) {
			this.sub_deleteContainerArgument = new DeleteNode({mapID, nodeID: withContainerArgument});
			this.sub_deleteContainerArgument.Validate_Early();
			await this.sub_deleteContainerArgument.Prepare();
		}
	}
	async Validate() {
		/*Assert((this.oldData.parents || {}).VKeys(true).length <= 1, "Cannot delete this child, as it has more than one parent. Try unlinking it instead.");
		let normalChildCount = (this.oldData.children || {}).VKeys(true).length;
		Assert(normalChildCount == 0, "Cannot delete this node until all its (non-impact-premise) children have been unlinked or deleted.");*/
		let earlyError = await GetAsync(()=>ForDelete_GetError(this.userInfo.id, this.oldData, this.payload.asPartOfMapDelete, this.asSubcommand));
		Assert(earlyError == null, earlyError);
		if (this.sub_deleteContainerArgument) await this.sub_deleteContainerArgument.Validate();
	}

	GetDBUpdates() {
		let {nodeID} = this.payload;
		let updates = {};<commit_msg>* Fixed that DeleteNode could error. (if maps were not all already loaded)<commit_after>import {GetAsync, GetAsync_Raw} from "Frame/Database/DatabaseHelpers";
import {GetNodeL2} from "Store/firebase/nodes/$node";
import {MapNodeRevision} from "Store/firebase/nodes/@MapNodeRevision";
import {Assert, ToInt} from "js-vextensions";
import {GetDataAsync} from "../../Frame/Database/DatabaseHelpers";
import {GetMaps} from "../../Store/firebase/maps";
import {GetNodeRevisions} from "../../Store/firebase/nodeRevisions";
import {GetNodeViewers} from "../../Store/firebase/nodeViewers";
import {ForDelete_GetError} from "../../Store/firebase/nodes";
import {MapNodeL2} from "../../Store/firebase/nodes/@MapNode";
import {Command, MergeDBUpdates} from "../Command";
import {MapEdit, UserEdit} from "../CommandMacros";

@MapEdit
@UserEdit
export default class DeleteNode extends Command<{mapID?: number, nodeID: number, withContainerArgument?: number, asPartOfMapDelete?: boolean}> {
	sub_deleteContainerArgument: DeleteNode;
	
	oldData: MapNodeL2;
	oldRevisions: MapNodeRevision[];
	oldParentChildrenOrders: number[][];
	viewerIDs_main: string[];
	mapIDs: number[];
	async Prepare() {
		let {mapID, nodeID, withContainerArgument, asPartOfMapDelete} = this.payload;
		this.oldData = await GetAsync_Raw(()=>GetNodeL2(nodeID));
		this.oldRevisions = await GetAsync(()=>GetNodeRevisions(nodeID));

		this.oldParentChildrenOrders = await Promise.all((this.oldData.parents || {}).VKeys().map(parentID=> {
			return GetDataAsync("nodes", parentID, "childrenOrder") as Promise<number[]>;
		}));

		this.viewerIDs_main = await GetAsync(()=>GetNodeViewers(nodeID));

		this.mapIDs = (await GetAsync(()=>GetMaps())).map(a=>a._id);

		if (withContainerArgument) {
			this.sub_deleteContainerArgument = new DeleteNode({mapID, nodeID: withContainerArgument});
			this.sub_deleteContainerArgument.Validate_Early();
			await this.sub_deleteContainerArgument.Prepare();
		}
	}
	async Validate() {
		/*Assert((this.oldData.parents || {}).VKeys(true).length <= 1, "Cannot delete this child, as it has more than one parent. Try unlinking it instead.");
		let normalChildCount = (this.oldData.children || {}).VKeys(true).length;
		Assert(normalChildCount == 0, "Cannot delete this node until all its (non-impact-premise) children have been unlinked or deleted.");*/
		let earlyError = await GetAsync(()=>ForDelete_GetError(this.userInfo.id, this.oldData, this.payload.asPartOfMapDelete, this.asSubcommand));
		Assert(earlyError == null, earlyError);
		if (this.sub_deleteContainerArgument) await this.sub_deleteContainerArgument.Validate();
	}

	GetDBUpdates() {
		let {nodeID} = this.payload;
		let updates = {};
{'fraction_non_alphanumeric': 0.15106423055189303, 'fraction_numerical': 0.002260312676586928, 'mean_word_length': 5.896103896103896, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '3293989', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1626, 'n_words': 374}