All files / src/lib/pricing calculator.ts

97% Statements 97/100
84.14% Branches 69/82
100% Functions 11/11
97.59% Lines 81/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                                                                                                                1x                       1x           37x   32x     30x                   30x 43x     30x 64x   30x       298x                     298x 249x             37x 5x   32x 2x     30x 27x 26x                   37x             37x                               37x   37x             37x 7x     37x           37x 37x 37x   37x     37x   37x 298x 298x   298x 298x 298x   298x     298x 298x     37x   37x 37x   37x         1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x           1x   1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x   1x 1x     1x     1x                     3x 3x   3x       3x       3x       3x                  
/**
 * Interior Design Cost Calculator — pure functions, no side effects.
 * All monetary values in INR (integer rupees).
 */
 
import { getCellRange, type Tier, type RoomType } from "./hyderabad-calibration";
 
// ─── Types ────────────────────────────────────────────────────────────────────
 
export type Zone =
	| "west"
	| "central"
	| "east"
	| "south"
	| "north"
	| "old-city";
 
export type Scope =
	| "kitchen-only"
	| "living-kitchen"
	| "full-home"
	| "custom";
 
export type BHK = 1 | 2 | 3 | 4 | 5;
 
export interface CalculatorInputs {
	bhk: BHK;
	tier: Tier;
	scope: Scope;
	zone: Zone;
	/** locality ID from taxonomy, e.g. "loc_gachibowli" — display only */
	locality: string;
	vastu: boolean;
}
 
export interface RoomEstimate {
	label: string;
	low: number;
	high: number;
}
 
export interface Estimate {
	rangeLow: number;
	rangeHigh: number;
	typical: number;
	byRoom: RoomEstimate[];
	/** [min, max] weeks */
	timelineWeeks: [number, number];
	included: string[];
	notIncluded: string[];
}
 
// ─── Zone multipliers ─────────────────────────────────────────────────────────
// Calibrated against Hyderabad market: Central premium areas cost ~30% more
// than South (base). Old City slightly below South due to logistics & access.
 
const ZONE_MULTIPLIER: Record<Zone, number> = {
	central: 1.3,
	west: 1.15,
	south: 1.0,
	east: 0.9,
	north: 0.9,
	"old-city": 0.85,
};
 
// ─── Vastu premium ────────────────────────────────────────────────────────────
// Vastu-compliant design adds ~8% for the extra design consultation & material
// adjustments (specific wood orientations, colour choices, false wall additions).
const VASTU_MULTIPLIER = 1.08;
 
// ─── Rooms included per scope × BHK ──────────────────────────────────────────
// Returns the list of room types to price for a given scope + BHK.
 
function roomsForScope(scope: Scope, bhk: BHK): RoomType[] {
	if (scope === "kitchen-only") return ["kitchen"];
 
	if (scope === "living-kitchen") return ["living", "kitchen", "dining"];
 
	// full-home and custom: all applicable rooms
	const rooms: RoomType[] = [
		"kitchen",
		"living",
		"dining",
		"master_bedroom",
		"pooja",
		"utility",
	];
 
	// Additional bedrooms beyond master
	const extraBedrooms = Math.max(0, bhk - 1);
	for (let i = 0; i < extraBedrooms; i++) rooms.push("bedroom");
 
	// Bathrooms: 1BHK = 1, 2BHK = 2, 3BHK = 2, 4BHK = 3, 5BHK = 4
	const bathroomCount = bhk === 1 ? 1 : bhk === 2 ? 2 : bhk === 3 ? 2 : bhk === 4 ? 3 : 4;
	for (let i = 0; i < bathroomCount; i++) rooms.push("bathroom");
 
	return rooms;
}
 
function roomLabel(room: RoomType, index: number): string {
	const labels: Record<RoomType, string> = {
		kitchen: "Kitchen",
		living: "Living Room",
		dining: "Dining Area",
		master_bedroom: "Master Bedroom",
		bedroom: "Bedroom",
		bathroom: "Bathroom",
		pooja: "Pooja Room",
		utility: "Utility Area",
	};
	// disambiguate repeated rooms (e.g. "Bedroom 2", "Bathroom 2")
	if (index > 0) return `${labels[room]} ${index + 1}`;
	return labels[room];
}
 
// ─── Timeline by scope × tier ─────────────────────────────────────────────────
// Weeks from design sign-off to handover. Does not include civil work.
 
function timelineFor(scope: Scope, tier: Tier): [number, number] {
	if (scope === "kitchen-only") {
		return tier === "luxury" ? [4, 8] : tier === "premium" ? [3, 6] : [2, 4];
	}
	if (scope === "living-kitchen") {
		return tier === "luxury" ? [8, 14] : tier === "premium" ? [6, 10] : [4, 8];
	}
	// full-home / custom
	if (tier === "luxury") return [16, 26];
	if (tier === "premium") return [12, 20];
	Eif (tier === "comfort") return [10, 16];
	return [8, 12];
}
 
// ─── Included / not-included bullets ─────────────────────────────────────────
 
function inclusionBullets(
	tier: Tier,
	scope: Scope,
): { included: string[]; notIncluded: string[] } {
	const base = [
		"Design consultation & 3D visualisations",
		"All furniture & woodwork mentioned in scope",
		"Hardware (hinges, channels) — branded per tier",
		"Installation & labour",
	];
 
	const tierExtras: Record<Tier, string[]> = {
		essentials: [],
		comfort: ["False ceiling in living & master bedroom", "Modular kitchen with chimney + hob"],
		premium: [
			"False ceiling throughout",
			"Wall panelling in living + master",
			"Branded kitchen appliances (Faber/Elica)",
		],
		luxury: [
			"Designer tiles & imported finishes",
			"Smart lighting (Philips Hue or equivalent)",
			"Full appliance package (built-in oven, dishwasher)",
			"3D walkthrough video before execution",
		],
	};
 
	const included = [...base, ...tierExtras[tier]];
 
	const notIncluded = [
		"Civil work (demolition, flooring, tiling, painting)",
		"Electrical & plumbing rewiring",
		"AC units & white goods (unless specified)",
		"GST (18% on design fees)",
	];
 
	if (scope !== "full-home" && scope !== "custom") {
		notIncluded.push("Rooms not in selected scope");
	}
 
	return { included, notIncluded };
}
 
// ─── Core compute ─────────────────────────────────────────────────────────────
 
export function computeEstimate(inputs: CalculatorInputs): Estimate {
	const { bhk, tier, scope, zone, vastu } = inputs;
	const zoneMult = ZONE_MULTIPLIER[zone];
	const vastuMult = vastu ? VASTU_MULTIPLIER : 1.0;
 
	const rooms = roomsForScope(scope, bhk);
 
	// Track how many times each room type has appeared (for labelling)
	const roomOccurrences: Partial<Record<RoomType, number>> = {};
 
	const byRoom: RoomEstimate[] = rooms.map((room) => {
		const occurrence = roomOccurrences[room] ?? 0;
		roomOccurrences[room] = occurrence + 1;
 
		const [rawLow, rawHigh] = getCellRange(bhk, tier, room);
		const low = Math.round(rawLow * zoneMult * vastuMult);
		const high = Math.round(rawHigh * zoneMult * vastuMult);
 
		return { label: roomLabel(room, occurrence), low, high };
	});
 
	const rangeLow = byRoom.reduce((sum, r) => sum + r.low, 0);
	const rangeHigh = byRoom.reduce((sum, r) => sum + r.high, 0);
	// Typical is a weighted median — roughly 40% of the way from low to high
	// (consistent with Indian market skewing towards lower-end of tier)
	const typical = Math.round(rangeLow + (rangeHigh - rangeLow) * 0.4);
 
	const timelineWeeks = timelineFor(scope, tier);
	const { included, notIncluded } = inclusionBullets(tier, scope);
 
	return { rangeLow, rangeHigh, typical, byRoom, timelineWeeks, included, notIncluded };
}
 
// ─── URL encode / decode ──────────────────────────────────────────────────────
 
const VALID_TIERS: Tier[] = ["essentials", "comfort", "premium", "luxury"];
const VALID_ZONES: Zone[] = ["west", "central", "east", "south", "north", "old-city"];
const VALID_SCOPES: Scope[] = ["kitchen-only", "living-kitchen", "full-home", "custom"];
 
export function encodeInputs(inputs: CalculatorInputs): URLSearchParams {
	const p = new URLSearchParams();
	p.set("bhk", String(inputs.bhk));
	p.set("tier", inputs.tier);
	p.set("scope", inputs.scope);
	p.set("zone", inputs.zone);
	Eif (inputs.locality) p.set("locality", inputs.locality);
	Eif (inputs.vastu) p.set("vastu", "yes");
	return p;
}
 
export function decodeInputs(
	params: URLSearchParams,
): Partial<CalculatorInputs> {
	const result: Partial<CalculatorInputs> = {};
 
	const bhkRaw = Number(params.get("bhk"));
	Eif ([1, 2, 3, 4, 5].includes(bhkRaw)) {
		result.bhk = bhkRaw as BHK;
	}
 
	const tier = params.get("tier");
	Eif (tier && VALID_TIERS.includes(tier as Tier)) {
		result.tier = tier as Tier;
	}
 
	const scope = params.get("scope");
	Eif (scope && VALID_SCOPES.includes(scope as Scope)) {
		result.scope = scope as Scope;
	}
 
	const zone = params.get("zone");
	Eif (zone && VALID_ZONES.includes(zone as Zone)) {
		result.zone = zone as Zone;
	}
 
	const locality = params.get("locality");
	Eif (locality) result.locality = locality;
 
	const vastu = params.get("vastu");
	if (vastu === "yes") result.vastu = Etrue;
	else if (vastu === "no") result.vastu = false;
 
	return result;
}
 
export const DEFAULT_INPUTS: CalculatorInputs = {
	bhk: 2,
	tier: "comfort",
	scope: "full-home",
	zone: "south",
	locality: "",
	vastu: false,
};
 
/** Merge partial decoded inputs with defaults, enforcing valid bounds */
export function clampInputs(partial: Partial<CalculatorInputs>): CalculatorInputs {
	const bhkRaw = partial.bhk ?? DEFAULT_INPUTS.bhk;
	const bhk: BHK = Math.min(5, Math.max(1, Math.round(bhkRaw))) as BHK;
 
	const tier: Tier = partial.tier && VALID_TIERS.includes(partial.tier)
		? partial.tier
		: DEFAULT_INPUTS.tier;
 
	const scope: Scope = partial.scope && VALID_SCOPES.includes(partial.scope)
		? partial.scope
		: DEFAULT_INPUTS.scope;
 
	const zone: Zone = partial.zone && VALID_ZONES.includes(partial.zone)
		? partial.zone
		: DEFAULT_INPUTS.zone;
 
	return {
		bhk,
		tier,
		scope,
		zone,
		locality: partial.locality ?? "",
		vastu: partial.vastu ?? false,
	};
}