// generated by diplomat-tool
import { DateDurationParseError } from "./DateDurationParseError.mjs"
import wasm from "./diplomat-wasm.mjs";
import * as diplomatRuntime from "./diplomat-runtime.mjs";



/**
 * See the [Rust documentation for `DateDuration`](https://docs.rs/icu/2.2.0/icu/calendar/types/struct.DateDuration.html) for more information.
 */
export class DateDuration {
    #isNegative;
    get isNegative() {
        return this.#isNegative;
    }
    set isNegative(value){
        this.#isNegative = value;
    }
    #years;
    get years() {
        return this.#years;
    }
    set years(value){
        this.#years = value;
    }
    #months;
    get months() {
        return this.#months;
    }
    set months(value){
        this.#months = value;
    }
    #weeks;
    get weeks() {
        return this.#weeks;
    }
    set weeks(value){
        this.#weeks = value;
    }
    #days;
    get days() {
        return this.#days;
    }
    set days(value){
        this.#days = value;
    }
    /** @internal */
    static fromFields(structObj) {
        return new DateDuration(structObj);
    }

    #internalConstructor(structObj) {
        if (typeof structObj !== "object") {
            throw new Error("DateDuration's constructor takes an object of DateDuration's fields.");
        }

        if ("isNegative" in structObj) {
            this.#isNegative = structObj.isNegative;
        } else {
            throw new Error("Missing required field isNegative.");
        }

        if ("years" in structObj) {
            this.#years = structObj.years;
        } else {
            throw new Error("Missing required field years.");
        }

        if ("months" in structObj) {
            this.#months = structObj.months;
        } else {
            throw new Error("Missing required field months.");
        }

        if ("weeks" in structObj) {
            this.#weeks = structObj.weeks;
        } else {
            throw new Error("Missing required field weeks.");
        }

        if ("days" in structObj) {
            this.#days = structObj.days;
        } else {
            throw new Error("Missing required field days.");
        }

        return this;
    }

    // Return this struct in FFI function friendly format.
    // Returns an array that can be expanded with spread syntax (...)
    _intoFFI(
        dst,
        functionCleanupArena,
        appendArrayMap
    ) {

        this._writeToArrayBuffer(wasm.memory.buffer, dst, functionCleanupArena, appendArrayMap);

        return dst;
    }

    static get _sizeBytes() {
        return 20;
    }

    /// Currently unused, we may want to use later on though:
    static get _sizeAlign() {
        return 4;
    }

    static _fromSuppliedValue(internalConstructor, obj) {
        if (internalConstructor !== diplomatRuntime.internalConstructor) {
            throw new Error("_fromSuppliedValue cannot be called externally.");
        }

        if (obj instanceof DateDuration) {
            return obj;
        }

        return DateDuration.fromFields(obj);
    }

    _writeToArrayBuffer(
        arrayBuffer,
        offset,
        functionCleanupArena,
        appendArrayMap
    ) {
        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#isNegative, Uint8Array);
        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 4, this.#years, Uint32Array);
        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 8, this.#months, Uint32Array);
        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 12, this.#weeks, Uint32Array);
        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 16, this.#days, Uint32Array);
    }

    // This struct contains borrowed fields, so this takes in a list of
    // "edges" corresponding to where each lifetime's data may have been borrowed from
    // and passes it down to individual fields containing the borrow.
    // This method does not attempt to handle any dependencies between lifetimes, the caller
    // should handle this when constructing edge arrays.
    static _fromFFI(internalConstructor, ptr) {
        if (internalConstructor !== diplomatRuntime.internalConstructor) {
            throw new Error("DateDuration._fromFFI is not meant to be called externally. Please use the default constructor.");
        }
        let structObj = {};
        const isNegativeDeref = (new Uint8Array(wasm.memory.buffer, ptr, 1))[0] === 1;
        structObj.isNegative = isNegativeDeref;
        const yearsDeref = (new Uint32Array(wasm.memory.buffer, ptr + 4, 1))[0];
        structObj.years = yearsDeref;
        const monthsDeref = (new Uint32Array(wasm.memory.buffer, ptr + 8, 1))[0];
        structObj.months = monthsDeref;
        const weeksDeref = (new Uint32Array(wasm.memory.buffer, ptr + 12, 1))[0];
        structObj.weeks = weeksDeref;
        const daysDeref = (new Uint32Array(wasm.memory.buffer, ptr + 16, 1))[0];
        structObj.days = daysDeref;

        return new DateDuration(structObj);
    }


    /**
     * Creates a new {@link DateDuration} from an ISO 8601 string.
     *
     * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/2.2.0/icu/calendar/types/struct.DateDuration.html#method.try_from_str) for more information.
     */
    static fromString(v) {
        let functionCleanupArena = new diplomatRuntime.CleanupArena();

        const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, v)));
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 21, 4, true);


        const result = wasm.icu4x_DateDuration_from_string_mv1(diplomatReceive.buffer, vSlice.ptr);

        try {
            if (!diplomatReceive.resultFlag) {
                const cause = new DateDurationParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
                throw new globalThis.Error('DateDurationParseError.' + cause.value, { cause });
            }
            return DateDuration._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            functionCleanupArena.free();

            diplomatReceive.free();
        }
    }

    /**
     * Returns a new {@link DateDuration} representing a number of years.
     *
     * See the [Rust documentation for `for_years`](https://docs.rs/icu/2.2.0/icu/calendar/types/struct.DateDuration.html#method.for_years) for more information.
     */
    static forYears(years) {
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 20, 4, false);


        const result = wasm.icu4x_DateDuration_for_years_mv1(diplomatReceive.buffer, years);

        try {
            return DateDuration._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            diplomatReceive.free();
        }
    }

    /**
     * Returns a new {@link DateDuration} representing a number of months.
     *
     * See the [Rust documentation for `for_months`](https://docs.rs/icu/2.2.0/icu/calendar/types/struct.DateDuration.html#method.for_months) for more information.
     */
    static forMonths(months) {
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 20, 4, false);


        const result = wasm.icu4x_DateDuration_for_months_mv1(diplomatReceive.buffer, months);

        try {
            return DateDuration._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            diplomatReceive.free();
        }
    }

    /**
     * Returns a new {@link DateDuration} representing a number of weeks.
     *
     * See the [Rust documentation for `for_weeks`](https://docs.rs/icu/2.2.0/icu/calendar/types/struct.DateDuration.html#method.for_weeks) for more information.
     */
    static forWeeks(weeks) {
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 20, 4, false);


        const result = wasm.icu4x_DateDuration_for_weeks_mv1(diplomatReceive.buffer, weeks);

        try {
            return DateDuration._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            diplomatReceive.free();
        }
    }

    /**
     * Returns a new {@link DateDuration} representing a number of days.
     *
     * See the [Rust documentation for `for_days`](https://docs.rs/icu/2.2.0/icu/calendar/types/struct.DateDuration.html#method.for_days) for more information.
     */
    static forDays(days) {
        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 20, 4, false);


        const result = wasm.icu4x_DateDuration_for_days_mv1(diplomatReceive.buffer, days);

        try {
            return DateDuration._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
        }

        finally {
            diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
            diplomatReceive.free();
        }
    }

    constructor(structObj) {
        return this.#internalConstructor(...arguments)
    }
}