import {
	front,
} from "./array_util";
import {
	computeNodeMass,
	getArticleName,
} from "./graph_utils";

/**
 * Create Nest3Part from vertex
 * Pre-condition: Vertex has to have assembly
 */
export function nest3Part(v: Vertex): Nest3Part | undefined {
	const assembly = (() => {
		const a = wsi4.node.assembly(v);
		if (a !== undefined) {
			return a;
		}
		const twoDimRep = wsi4.node.twoDimRep(v);
		if (twoDimRep === undefined) {
			return undefined;
		}
		const iops = wsi4.cam.util.extractInnerOuterPolygons(twoDimRep);
		if (iops.length !== 1) {
			return undefined;
		}
		const t = wsi4.node.sheetThickness(v);
		if (t === undefined) {
			return undefined;
		}
		return wsi4.geo.assembly.fromIop(front(iops), t, getArticleName(v));
	})();
	if (assembly === undefined) {
		return undefined;
	}
	const mass = computeNodeMass(v);
	if (mass === undefined) {
		return undefined;
	}
	return {
		assembly: assembly,
		multiplicity: wsi4.node.multiplicity(v),
		mass: mass,
		profileShadow: wsi4.node.asyncProfileShadow(v),
	};
}
