arky-sdk 0.3.84 → 0.3.86

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.cjs CHANGED
@@ -1712,8 +1712,186 @@ async function injectSvgIntoElement(mediaObject, targetElement, className) {
1712
1712
  }
1713
1713
  }
1714
1714
 
1715
+ // src/utils/time.ts
1716
+ function formatTime(ts, tz) {
1717
+ return new Date(ts * 1e3).toLocaleTimeString([], {
1718
+ hour: "2-digit",
1719
+ minute: "2-digit",
1720
+ timeZone: tz
1721
+ });
1722
+ }
1723
+ function formatSlotTime(from, to, tz) {
1724
+ return `${formatTime(from, tz)} \u2013 ${formatTime(to, tz)}`;
1725
+ }
1726
+ function getTzOffset(date, tz) {
1727
+ const utc = new Date(date.toLocaleString("en-US", { timeZone: "UTC" }));
1728
+ const local = new Date(date.toLocaleString("en-US", { timeZone: tz }));
1729
+ return (utc.getTime() - local.getTime()) / 6e4;
1730
+ }
1731
+ function toUtcTimestamp(year, month, day, mins, tz) {
1732
+ const midnight = new Date(Date.UTC(year, month - 1, day));
1733
+ const offset = getTzOffset(midnight, tz);
1734
+ return Math.floor(midnight.getTime() / 1e3) + (mins + offset) * 60;
1735
+ }
1736
+ function formatDateDisplay(dateStr, tz) {
1737
+ if (!dateStr) return "";
1738
+ const date = /* @__PURE__ */ new Date(dateStr + "T00:00:00");
1739
+ return date.toLocaleDateString("en-US", {
1740
+ weekday: "short",
1741
+ month: "short",
1742
+ day: "numeric",
1743
+ timeZone: tz
1744
+ });
1745
+ }
1746
+ function getIsoDate(date, tz) {
1747
+ return date.toLocaleDateString("en-CA", { timeZone: tz });
1748
+ }
1749
+ function parseIsoDate(isoDate) {
1750
+ const [year, month, day] = isoDate.split("-").map(Number);
1751
+ return { year, month, day };
1752
+ }
1753
+
1754
+ // src/utils/slots.ts
1755
+ function getTotalDuration(durations) {
1756
+ return durations.reduce((sum, d) => sum + d.duration, 0);
1757
+ }
1758
+ function isBlocked(from, to, timeline, limit) {
1759
+ const before = timeline.filter((p) => p.timestamp <= from).sort((a, b) => b.timestamp - a.timestamp);
1760
+ if (before.length > 0 && before[0].concurrent >= limit) return true;
1761
+ for (const p of timeline) {
1762
+ if (p.timestamp >= from && p.timestamp < to && p.concurrent >= limit) {
1763
+ return true;
1764
+ }
1765
+ }
1766
+ return false;
1767
+ }
1768
+ function getWorkingHoursForDate(wt, date, tz) {
1769
+ if (!wt) return [];
1770
+ const dayName = date.toLocaleDateString("en-US", { weekday: "long", timeZone: tz }).toLowerCase();
1771
+ const m = date.getMonth() + 1;
1772
+ const d = date.getDate();
1773
+ const ts = Math.floor(date.getTime() / 1e3);
1774
+ const specific = wt.specificDates?.find((s) => s.date === ts);
1775
+ if (specific) return specific.workingHours || [];
1776
+ const outcast = wt.outcastDates?.find((o) => o.month === m && o.day === d);
1777
+ if (outcast) return outcast.workingHours || [];
1778
+ return wt.workingDays?.find((w) => w.day === dayName)?.workingHours || [];
1779
+ }
1780
+ function computeSlotsForDate(opts) {
1781
+ const { providers, date, durations, timezone, slotInterval } = opts;
1782
+ const total = getTotalDuration(durations);
1783
+ const interval = slotInterval || total;
1784
+ const slots = [];
1785
+ const nowTs = Math.floor(Date.now() / 1e3);
1786
+ const today = /* @__PURE__ */ new Date();
1787
+ today.setHours(0, 0, 0, 0);
1788
+ if (date < today) return [];
1789
+ const [year, month, day] = date.toLocaleDateString("en-CA", { timeZone: timezone }).split("-").map(Number);
1790
+ for (const provider of providers) {
1791
+ const workingHours = getWorkingHoursForDate(provider.workingTime, date, timezone);
1792
+ for (const wh of workingHours) {
1793
+ for (let m = wh.from; m + total <= wh.to; m += interval) {
1794
+ const from = toUtcTimestamp(year, month, day, m, timezone);
1795
+ const to = from + total * 60;
1796
+ if (from < nowTs) continue;
1797
+ if (!isBlocked(from, to, provider.timeline, provider.concurrentLimit)) {
1798
+ slots.push({ from, to, providerId: provider.id });
1799
+ }
1800
+ }
1801
+ }
1802
+ }
1803
+ return slots.sort((a, b) => a.from - b.from);
1804
+ }
1805
+ function hasAvailableSlots(opts) {
1806
+ return computeSlotsForDate(opts).length > 0;
1807
+ }
1808
+ function buildCalendar(opts) {
1809
+ const {
1810
+ currentMonth,
1811
+ selectedDate,
1812
+ startDate,
1813
+ endDate,
1814
+ providers,
1815
+ selectedProvider,
1816
+ durations,
1817
+ timezone,
1818
+ isMultiDay
1819
+ } = opts;
1820
+ const year = currentMonth.getFullYear();
1821
+ const month = currentMonth.getMonth();
1822
+ const first = new Date(year, month, 1);
1823
+ const last = new Date(year, month + 1, 0);
1824
+ const today = /* @__PURE__ */ new Date();
1825
+ today.setHours(0, 0, 0, 0);
1826
+ const cells = [];
1827
+ const pad = (first.getDay() + 6) % 7;
1828
+ for (let i = 0; i < pad; i++) {
1829
+ cells.push({
1830
+ date: /* @__PURE__ */ new Date(0),
1831
+ iso: "",
1832
+ available: false,
1833
+ isSelected: false,
1834
+ isInRange: false,
1835
+ isToday: false,
1836
+ blank: true
1837
+ });
1838
+ }
1839
+ const activeProviders = selectedProvider ? providers.filter((p) => p.id === selectedProvider.id) : providers;
1840
+ for (let d = 1; d <= last.getDate(); d++) {
1841
+ const date = new Date(year, month, d);
1842
+ const iso = `${year}-${String(month + 1).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
1843
+ const available = activeProviders.length > 0 && hasAvailableSlots({ providers: activeProviders, date, durations, timezone });
1844
+ const isToday = date.getTime() === today.getTime();
1845
+ let isSelected = false;
1846
+ let isInRange = false;
1847
+ if (isMultiDay) {
1848
+ isSelected = iso === startDate || iso === endDate;
1849
+ if (startDate && endDate) {
1850
+ isInRange = iso > startDate && iso < endDate;
1851
+ }
1852
+ } else {
1853
+ isSelected = iso === selectedDate;
1854
+ }
1855
+ cells.push({
1856
+ date,
1857
+ iso,
1858
+ available,
1859
+ isSelected,
1860
+ isInRange,
1861
+ isToday,
1862
+ blank: false
1863
+ });
1864
+ }
1865
+ return cells;
1866
+ }
1867
+ function getMonthYear(date) {
1868
+ return date.toLocaleDateString("en-US", { month: "long", year: "numeric" });
1869
+ }
1870
+
1871
+ // src/utils/promo.ts
1872
+ function mapQuoteError(code, fallback) {
1873
+ switch (code) {
1874
+ case "PROMO.MIN_ORDER":
1875
+ return fallback || "Promo requires a higher minimum order.";
1876
+ case "PROMO.NOT_ACTIVE":
1877
+ return "Promo code is not active.";
1878
+ case "PROMO.NOT_YET_VALID":
1879
+ return "Promo code is not yet valid.";
1880
+ case "PROMO.EXPIRED":
1881
+ return "Promo code has expired.";
1882
+ case "PROMO.MAX_USES":
1883
+ return "Promo code usage limit exceeded.";
1884
+ case "PROMO.MAX_USES_PER_USER":
1885
+ return "You have already used this promo code.";
1886
+ case "PROMO.NOT_FOUND":
1887
+ return "Promo code not found.";
1888
+ default:
1889
+ return fallback || "Failed to fetch quote.";
1890
+ }
1891
+ }
1892
+
1715
1893
  // src/index.ts
1716
- var SDK_VERSION = "0.3.84";
1894
+ var SDK_VERSION = "0.3.86";
1717
1895
  var SUPPORTED_FRAMEWORKS = [
1718
1896
  "astro",
1719
1897
  "react",
@@ -1814,7 +1992,25 @@ async function createArkySDK(config) {
1814
1992
  // SVG utilities
1815
1993
  getSvgContentForAstro,
1816
1994
  fetchSvgContent,
1817
- injectSvgIntoElement
1995
+ injectSvgIntoElement,
1996
+ // Time utilities
1997
+ formatTime,
1998
+ formatSlotTime,
1999
+ getTzOffset,
2000
+ toUtcTimestamp,
2001
+ formatDateDisplay,
2002
+ getIsoDate,
2003
+ parseIsoDate,
2004
+ // Slot computation utilities
2005
+ computeSlotsForDate,
2006
+ hasAvailableSlots,
2007
+ buildCalendar,
2008
+ getMonthYear,
2009
+ getTotalDuration,
2010
+ isBlocked,
2011
+ getWorkingHoursForDate,
2012
+ // Promo utilities
2013
+ mapQuoteError
1818
2014
  }
1819
2015
  };
1820
2016
  return sdk;