• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { DataError } from "./DataError.mjs"
3import { DataProvider } from "./DataProvider.mjs"
4import { Locale } from "./Locale.mjs"
5import { Weekday } from "./Weekday.mjs"
6import { WeekendContainsDay } from "./WeekendContainsDay.mjs"
7import wasm from "./diplomat-wasm.mjs";
8import * as diplomatRuntime from "./diplomat-runtime.mjs";
9
10
11/** A Week calculator, useful to be passed in to `week_of_year()` on Date and DateTime types
12*
13*See the [Rust documentation for `WeekCalculator`](https://docs.rs/icu/latest/icu/calendar/week/struct.WeekCalculator.html) for more information.
14*/
15const WeekCalculator_box_destroy_registry = new FinalizationRegistry((ptr) => {
16    wasm.icu4x_WeekCalculator_destroy_mv1(ptr);
17});
18
19export class WeekCalculator {
20
21    // Internal ptr reference:
22    #ptr = null;
23
24    // Lifetimes are only to keep dependencies alive.
25    // Since JS won't garbage collect until there are no incoming edges.
26    #selfEdge = [];
27
28    #internalConstructor(symbol, ptr, selfEdge) {
29        if (symbol !== diplomatRuntime.internalConstructor) {
30            console.error("WeekCalculator is an Opaque type. You cannot call its constructor.");
31            return;
32        }
33
34        this.#ptr = ptr;
35        this.#selfEdge = selfEdge;
36
37        // Are we being borrowed? If not, we can register.
38        if (this.#selfEdge.length === 0) {
39            WeekCalculator_box_destroy_registry.register(this, this.#ptr);
40        }
41
42        return this;
43    }
44    get ffiValue() {
45        return this.#ptr;
46    }
47
48    #defaultConstructor(locale) {
49        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
50
51        const result = wasm.icu4x_WeekCalculator_create_mv1(diplomatReceive.buffer, locale.ffiValue);
52
53        try {
54            if (!diplomatReceive.resultFlag) {
55                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
56                throw new globalThis.Error('DataError: ' + cause.value, { cause });
57            }
58            return new WeekCalculator(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
59        }
60
61        finally {
62            diplomatReceive.free();
63        }
64    }
65
66    static createWithProvider(provider, locale) {
67        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
68
69        const result = wasm.icu4x_WeekCalculator_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue);
70
71        try {
72            if (!diplomatReceive.resultFlag) {
73                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
74                throw new globalThis.Error('DataError: ' + cause.value, { cause });
75            }
76            return new WeekCalculator(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
77        }
78
79        finally {
80            diplomatReceive.free();
81        }
82    }
83
84    static fromFirstDayOfWeekAndMinWeekDays(firstWeekday, minWeekDays) {
85        const result = wasm.icu4x_WeekCalculator_from_first_day_of_week_and_min_week_days_mv1(firstWeekday.ffiValue, minWeekDays);
86
87        try {
88            return new WeekCalculator(diplomatRuntime.internalConstructor, result, []);
89        }
90
91        finally {}
92    }
93
94    get firstWeekday() {
95        const result = wasm.icu4x_WeekCalculator_first_weekday_mv1(this.ffiValue);
96
97        try {
98            return new Weekday(diplomatRuntime.internalConstructor, result);
99        }
100
101        finally {}
102    }
103
104    get minWeekDays() {
105        const result = wasm.icu4x_WeekCalculator_min_week_days_mv1(this.ffiValue);
106
107        try {
108            return result;
109        }
110
111        finally {}
112    }
113
114    get weekend() {
115        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 7, 1, false);
116
117        const result = wasm.icu4x_WeekCalculator_weekend_mv1(diplomatReceive.buffer, this.ffiValue);
118
119        try {
120            return WeekendContainsDay._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
121        }
122
123        finally {
124            diplomatReceive.free();
125        }
126    }
127
128    constructor(locale) {
129        if (arguments[0] === diplomatRuntime.exposeConstructor) {
130            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
131        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
132            return this.#internalConstructor(...arguments);
133        } else {
134            return this.#defaultConstructor(...arguments);
135        }
136    }
137}