@final-commerce/common 2.12.0-staging.1 → 2.12.0-staging.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -3828,6 +3828,70 @@ function windowForPeriods({ rule, timeZone, startAt, periods, now }) {
3828
3828
  slotsForWindow(grid, startAt, endAt);
3829
3829
  return { startAt, endAt };
3830
3830
  }
3831
+ function rentalEnds(input) {
3832
+ const { rule, timeZone, startAt, now, untilDay } = input;
3833
+ const parts = bookingRuleParts(rule);
3834
+ if (parts.method !== "date-ranges") throw new BookingRulesError("Only date-range rules book a stay");
3835
+ const horizon = shopDayRange(untilDay, 1, timeZone).to;
3836
+ if (horizon <= startAt) throw new BookingRulesError(`untilDay ${untilDay} is before the stay starts`);
3837
+ const gridUntil = (end) => buildGrid(rule, timeZone, startAt, new Date(Math.min(end, startAt.getTime() + 366 * 1440 * 6e4)), now);
3838
+ let walk = walkRentalEnds(gridUntil(horizon.getTime() + 1440 * 6e4), input, parts, horizon);
3839
+ if (!walk.ends.length && walk.refusal && walk.ranOut)
3840
+ walk = walkRentalEnds(gridUntil(Infinity), input, parts, horizon);
3841
+ if (walk.ends.length) return walk.ends;
3842
+ throw walk.refusal ?? new BookingRulesError(
3843
+ `No stay from ${startAt.toISOString()} fits these rules: the bookable days run out before the shortest allowed stay`
3844
+ );
3845
+ }
3846
+ function walkRentalEnds(grid, { rule, timeZone, startAt, untilDay }, parts, horizon) {
3847
+ const startIndex = grid.findIndex((cell) => cell.startAt.getTime() === startAt.getTime());
3848
+ if (startIndex < 0) {
3849
+ throw new BookingRulesError(`${startAt.toISOString()} is not a bookable slot start for these rules`);
3850
+ }
3851
+ slotsForWindow(grid, startAt, grid[startIndex].endAt);
3852
+ const ends = [];
3853
+ let refusal;
3854
+ for (const cell of grid.slice(startIndex)) {
3855
+ if (cell.endAt > horizon && (ends.length || !refusal)) {
3856
+ refusal ??= new BookingRulesError(`No stay from ${startAt.toISOString()} ends on or before ${untilDay}`);
3857
+ return { ends, refusal, ranOut: false };
3858
+ }
3859
+ const slots = refusalOr(() => slotsForWindow(grid, startAt, cell.endAt));
3860
+ if (slots instanceof BookingRulesError) return { ends, refusal, ranOut: false };
3861
+ const periods = refusalOr(
3862
+ () => parts.wholePeriods ? wholePeriodsIn(rule.ratePeriod, timeZone, startAt, cell.endAt, slots.length) : slots.length
3863
+ );
3864
+ if (periods instanceof BookingRulesError) continue;
3865
+ const stay = refusalOr(() => checkStay(parts, timeZone, startAt, periods));
3866
+ if (stay instanceof BookingRulesError) {
3867
+ if (ends.length) return { ends, refusal, ranOut: false };
3868
+ refusal = stay;
3869
+ continue;
3870
+ }
3871
+ ends.push(rentalEndOf(slots, periods, timeZone));
3872
+ }
3873
+ return { ends, refusal, ranOut: true };
3874
+ }
3875
+ function rentalEndOf(slots, periods, timeZone) {
3876
+ const endAt = slots[slots.length - 1].endAt;
3877
+ return {
3878
+ periods,
3879
+ endAt: endAt.toISOString(),
3880
+ lastDayKey: shopDayKey(new Date(endAt.getTime() - 1), timeZone),
3881
+ coveredSlots: slots.map((slot) => ({
3882
+ startAt: slot.startAt.toISOString(),
3883
+ dayKey: shopDayKey(slot.startAt, timeZone)
3884
+ }))
3885
+ };
3886
+ }
3887
+ function refusalOr(run) {
3888
+ try {
3889
+ return run();
3890
+ } catch (error) {
3891
+ if (error instanceof BookingRulesError) return error;
3892
+ throw error;
3893
+ }
3894
+ }
3831
3895
  function sameDayMonthsLater(startAt, months, timeZone) {
3832
3896
  const { year, month, day } = zonedParts(startAt, timeZone);
3833
3897
  return instantFromLocal(...sameDayMonthsLaterDate(year, month, day, months), 0, timeZone);
@@ -5045,6 +5109,7 @@ export {
5045
5109
  priceForTotal,
5046
5110
  provincesForPostCode,
5047
5111
  removeFragment,
5112
+ rentalEnds,
5048
5113
  resolveFamily,
5049
5114
  resolveModifiersForProduct,
5050
5115
  resolveShipping,