//#region src/errors.ts
var FulfillmentValidationError = class extends Error {
	field;
	name = "FulfillmentValidationError";
	code = "FULFILLMENT_VALIDATION";
	constructor(field) {
		super(`fulfillment validation failed: ${field}`);
		this.field = field;
	}
};
var VoucherAlreadyRedeemedError = class extends Error {
	voucherId;
	name = "VoucherAlreadyRedeemedError";
	code = "VOUCHER_ALREADY_REDEEMED";
	httpStatus = 409;
	constructor(voucherId) {
		super(`voucher already redeemed: ${voucherId}`);
		this.voucherId = voucherId;
	}
};
var VoucherExpiredError = class extends Error {
	voucherId;
	name = "VoucherExpiredError";
	code = "VOUCHER_EXPIRED";
	httpStatus = 422;
	constructor(voucherId) {
		super(`voucher expired: ${voucherId}`);
		this.voucherId = voucherId;
	}
};
var VoucherWrongVendorError = class extends Error {
	context;
	name = "VoucherWrongVendorError";
	code = "VOUCHER_WRONG_VENDOR";
	httpStatus = 403;
	constructor(context) {
		super(`voucher wrong vendor: ${context.voucherId} (scanning ${context.scanningVendorId})`);
		this.context = context;
	}
};
var LabelPurchaseError = class extends Error {
	shipmentId;
	detail;
	name = "LabelPurchaseError";
	code = "LABEL_PURCHASE";
	httpStatus = 502;
	constructor(shipmentId, detail) {
		super(`label purchase failed for shipment ${shipmentId}: ${detail}`);
		this.shipmentId = shipmentId;
		this.detail = detail;
	}
};
var UnfulfillableLineError = class extends Error {
	context;
	name = "UnfulfillableLineError";
	code = "UNFULFILLABLE_LINE";
	httpStatus = 422;
	constructor(context) {
		super(`unfulfillable line: order ${context.orderId}, line ${context.lineId}, kind ${context.kind}`);
		this.context = context;
	}
};
var DownloadNotFoundError = class extends Error {
	grantId;
	name = "DownloadNotFoundError";
	code = "DOWNLOAD_NOT_FOUND";
	httpStatus = 404;
	constructor(grantId) {
		super(`download not found: ${grantId}`);
		this.grantId = grantId;
	}
};
function isFulfillmentValidationError(e) {
	return typeof e === "object" && e !== null && e.name === "FulfillmentValidationError" && e.code === "FULFILLMENT_VALIDATION";
}
function isVoucherAlreadyRedeemedError(e) {
	return typeof e === "object" && e !== null && e.name === "VoucherAlreadyRedeemedError" && e.code === "VOUCHER_ALREADY_REDEEMED";
}
function isVoucherExpiredError(e) {
	return typeof e === "object" && e !== null && e.name === "VoucherExpiredError" && e.code === "VOUCHER_EXPIRED";
}
function isVoucherWrongVendorError(e) {
	return typeof e === "object" && e !== null && e.name === "VoucherWrongVendorError" && e.code === "VOUCHER_WRONG_VENDOR";
}
function isLabelPurchaseError(e) {
	return typeof e === "object" && e !== null && e.name === "LabelPurchaseError" && e.code === "LABEL_PURCHASE";
}
function isUnfulfillableLineError(e) {
	return typeof e === "object" && e !== null && e.name === "UnfulfillableLineError" && e.code === "UNFULFILLABLE_LINE";
}
function isDownloadNotFoundError(e) {
	return typeof e === "object" && e !== null && e.name === "DownloadNotFoundError" && e.code === "DOWNLOAD_NOT_FOUND";
}
//#endregion
//#region src/types.ts
const VOUCHER_STATES = [
	"UNREDEEMED",
	"REDEEMED",
	"EXPIRED",
	"CANCELLED"
];
const SHIPMENT_STATUSES = [
	"pending",
	"labeled",
	"in_transit",
	"delivered",
	"failed"
];
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
function assertUuid(value, field) {
	if (!UUID_RE.test(value)) throw new FulfillmentValidationError(field);
}
function assertPositiveInt(qty, field = "qty") {
	if (!Number.isInteger(qty) || qty <= 0) throw new FulfillmentValidationError(field);
}
function normalizeEmail(email) {
	return email.trim().toLowerCase();
}
function ownerKeyOf(ref) {
	if (ref.userId) return `user:${ref.userId}`;
	if (ref.guestEmail) return `email:${normalizeEmail(ref.guestEmail)}`;
	throw new FulfillmentValidationError("owner");
}
//#endregion
export { isLabelPurchaseError as _, assertUuid as a, isVoucherExpiredError as b, DownloadNotFoundError as c, UnfulfillableLineError as d, VoucherAlreadyRedeemedError as f, isFulfillmentValidationError as g, isDownloadNotFoundError as h, assertPositiveInt as i, FulfillmentValidationError as l, VoucherWrongVendorError as m, UUID_RE as n, normalizeEmail as o, VoucherExpiredError as p, VOUCHER_STATES as r, ownerKeyOf as s, SHIPMENT_STATUSES as t, LabelPurchaseError as u, isUnfulfillableLineError as v, isVoucherWrongVendorError as x, isVoucherAlreadyRedeemedError as y };
