49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
import { buildGeometry } from './geometry';
|
|
import { planFlooringOptions } from './flooring';
|
|
import { planSkirting } from './skirting';
|
|
import type { FlooringOptions } from './flooring';
|
|
import type { Geometry, Project, SkirtingPlan } from './types';
|
|
|
|
/** One calculator per editor. Compare calculation inputs, excluding names and progress. */
|
|
export function createPlanCalculator() {
|
|
let previous: Project | undefined;
|
|
let geometries: Geometry[] = [];
|
|
let flooringOptions: FlooringOptions;
|
|
let skirting: SkirtingPlan;
|
|
return (project: Project) => {
|
|
const sameOutlines =
|
|
previous &&
|
|
previous.tolerance === project.tolerance &&
|
|
previous.rooms.length === project.rooms.length &&
|
|
project.rooms.every(
|
|
(room, i) => room.id === previous!.rooms[i].id && room.walls === previous!.rooms[i].walls,
|
|
);
|
|
const sameDirections =
|
|
sameOutlines &&
|
|
project.rooms.every((room, i) => room.direction === previous!.rooms[i].direction);
|
|
const sameFlooring =
|
|
previous &&
|
|
Object.entries(project.flooring).every(
|
|
([key, value]) => key === 'optimisation' || Reflect.get(previous!.flooring, key) === value,
|
|
);
|
|
if (!sameOutlines)
|
|
geometries = project.rooms.map((room, i) =>
|
|
previous?.tolerance === project.tolerance &&
|
|
previous.rooms[i]?.id === room.id &&
|
|
previous.rooms[i]?.walls === room.walls
|
|
? geometries[i]
|
|
: buildGeometry(room, project.tolerance),
|
|
);
|
|
if (!sameDirections || !sameFlooring)
|
|
flooringOptions = planFlooringOptions(
|
|
project.rooms,
|
|
project.flooring,
|
|
project.tolerance,
|
|
geometries,
|
|
);
|
|
if (!sameOutlines || previous?.skirting !== project.skirting)
|
|
skirting = planSkirting(project.rooms, project.skirting, project.tolerance, geometries);
|
|
previous = project;
|
|
return { geometries, flooringOptions: flooringOptions!, skirting: skirting! };
|
|
};
|
|
}
|