"use strict";
// This file was auto-generated by Fern from our API Definition.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BenefitsClient = void 0;
const BaseClient_1 = require("../../../../../../BaseClient");
const core = __importStar(require("../../../../../../core"));
const headers_1 = require("../../../../../../core/headers");
const environments = __importStar(require("../../../../../../environments"));
const handleNonStatusCodeError_1 = require("../../../../../../errors/handleNonStatusCodeError");
const errors = __importStar(require("../../../../../../errors/index"));
const serializers = __importStar(require("../../../../../../serialization/index"));
class BenefitsClient {
    constructor(options) {
        this._options = (0, BaseClient_1.normalizeClientOptionsWithAuth)(options);
    }
    /**
     * Returns a list of `Benefit` objects.
     *
     * @param {Merge.hris.BenefitsListRequest} request
     * @param {BenefitsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.hris.benefits.list({
     *         createdAfter: new Date("2024-01-15T09:30:00.000Z"),
     *         createdBefore: new Date("2024-01-15T09:30:00.000Z"),
     *         cursor: "cD0yMDIxLTAxLTA2KzAzJTNBMjQlM0E1My40MzQzMjYlMkIwMCUzQTAw",
     *         employeeId: "employee_id",
     *         includeDeletedData: true,
     *         includeRemoteData: true,
     *         includeShellData: true,
     *         modifiedAfter: new Date("2024-01-15T09:30:00.000Z"),
     *         modifiedBefore: new Date("2024-01-15T09:30:00.000Z"),
     *         pageSize: 1,
     *         remoteId: "remote_id"
     *     })
     */
    list() {
        return __awaiter(this, arguments, void 0, function* (request = {}, requestOptions) {
            const list = core.HttpResponsePromise.interceptFunction((request) => __awaiter(this, void 0, void 0, function* () {
                var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
                const { createdAfter, createdBefore, cursor, employeeId, expand, includeDeletedData, includeRemoteData, includeShellData, modifiedAfter, modifiedBefore, pageSize, remoteId, } = request;
                const _queryParams = {
                    created_after: createdAfter === null || createdAfter === void 0 ? void 0 : createdAfter.toISOString(),
                    created_before: createdBefore === null || createdBefore === void 0 ? void 0 : createdBefore.toISOString(),
                    cursor,
                    employee_id: employeeId,
                    expand: Array.isArray(expand) ? expand.map((item) => item) : expand != null ? expand : undefined,
                    include_deleted_data: includeDeletedData,
                    include_remote_data: includeRemoteData,
                    include_shell_data: includeShellData,
                    modified_after: modifiedAfter === null || modifiedAfter === void 0 ? void 0 : modifiedAfter.toISOString(),
                    modified_before: modifiedBefore === null || modifiedBefore === void 0 ? void 0 : modifiedBefore.toISOString(),
                    page_size: pageSize,
                    remote_id: remoteId,
                };
                const _authRequest = yield this._options.authProvider.getAuthRequest();
                const _headers = (0, headers_1.mergeHeaders)(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, (0, headers_1.mergeOnlyDefinedHeaders)({
                    "X-Account-Token": (_b = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.accountToken) !== null && _b !== void 0 ? _b : (_c = this._options) === null || _c === void 0 ? void 0 : _c.accountToken,
                }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
                const _response = yield ((_d = this._options.fetcher) !== null && _d !== void 0 ? _d : core.fetcher)({
                    url: core.url.join((_f = (_e = (yield core.Supplier.get(this._options.baseUrl))) !== null && _e !== void 0 ? _e : (yield core.Supplier.get(this._options.environment))) !== null && _f !== void 0 ? _f : environments.MergeEnvironment.Production, "hris/v1/benefits"),
                    method: "GET",
                    headers: _headers,
                    queryParameters: Object.assign(Object.assign({}, _queryParams), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams),
                    timeoutMs: ((_j = (_g = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _g !== void 0 ? _g : (_h = this._options) === null || _h === void 0 ? void 0 : _h.timeoutInSeconds) !== null && _j !== void 0 ? _j : 60) * 1000,
                    maxRetries: (_k = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _k !== void 0 ? _k : (_l = this._options) === null || _l === void 0 ? void 0 : _l.maxRetries,
                    abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
                    fetchFn: (_m = this._options) === null || _m === void 0 ? void 0 : _m.fetch,
                    logging: this._options.logging,
                });
                if (_response.ok) {
                    return {
                        data: serializers.hris.PaginatedBenefitList.parseOrThrow(_response.body, {
                            unrecognizedObjectKeys: "passthrough",
                            allowUnrecognizedUnionMembers: true,
                            allowUnrecognizedEnumValues: true,
                            skipValidation: true,
                            breadcrumbsPrefix: ["response"],
                        }),
                        rawResponse: _response.rawResponse,
                    };
                }
                if (_response.error.reason === "status-code") {
                    throw new errors.MergeError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.body,
                        rawResponse: _response.rawResponse,
                    });
                }
                return (0, handleNonStatusCodeError_1.handleNonStatusCodeError)(_response.error, _response.rawResponse, "GET", "/hris/v1/benefits");
            }));
            const dataWithRawResponse = yield list(request).withRawResponse();
            return new core.Page({
                response: dataWithRawResponse.data,
                rawResponse: dataWithRawResponse.rawResponse,
                hasNextPage: (response) => (response === null || response === void 0 ? void 0 : response.next) != null && !(typeof (response === null || response === void 0 ? void 0 : response.next) === "string" && (response === null || response === void 0 ? void 0 : response.next) === ""),
                getItems: (response) => { var _a; return (_a = response === null || response === void 0 ? void 0 : response.results) !== null && _a !== void 0 ? _a : []; },
                loadPage: (response) => {
                    return list(core.setObjectProperty(request, "cursor", response === null || response === void 0 ? void 0 : response.next));
                },
            });
        });
    }
    /**
     * Returns a `Benefit` object with the given `id`.
     *
     * @param {string} id
     * @param {Merge.hris.BenefitsRetrieveRequest} request
     * @param {BenefitsClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.hris.benefits.retrieve("id", {
     *         includeRemoteData: true,
     *         includeShellData: true
     *     })
     */
    retrieve(id, request = {}, requestOptions) {
        return core.HttpResponsePromise.fromPromise(this.__retrieve(id, request, requestOptions));
    }
    __retrieve(id_1) {
        return __awaiter(this, arguments, void 0, function* (id, request = {}, requestOptions) {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
            const { expand, includeRemoteData, includeShellData } = request;
            const _queryParams = {
                expand: Array.isArray(expand) ? expand.map((item) => item) : expand != null ? expand : undefined,
                include_remote_data: includeRemoteData,
                include_shell_data: includeShellData,
            };
            const _authRequest = yield this._options.authProvider.getAuthRequest();
            const _headers = (0, headers_1.mergeHeaders)(_authRequest.headers, (_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, (0, headers_1.mergeOnlyDefinedHeaders)({ "X-Account-Token": (_b = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.accountToken) !== null && _b !== void 0 ? _b : (_c = this._options) === null || _c === void 0 ? void 0 : _c.accountToken }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
            const _response = yield ((_d = this._options.fetcher) !== null && _d !== void 0 ? _d : core.fetcher)({
                url: core.url.join((_f = (_e = (yield core.Supplier.get(this._options.baseUrl))) !== null && _e !== void 0 ? _e : (yield core.Supplier.get(this._options.environment))) !== null && _f !== void 0 ? _f : environments.MergeEnvironment.Production, `hris/v1/benefits/${core.url.encodePathParam(id)}`),
                method: "GET",
                headers: _headers,
                queryParameters: Object.assign(Object.assign({}, _queryParams), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams),
                timeoutMs: ((_j = (_g = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _g !== void 0 ? _g : (_h = this._options) === null || _h === void 0 ? void 0 : _h.timeoutInSeconds) !== null && _j !== void 0 ? _j : 60) * 1000,
                maxRetries: (_k = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _k !== void 0 ? _k : (_l = this._options) === null || _l === void 0 ? void 0 : _l.maxRetries,
                abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
                fetchFn: (_m = this._options) === null || _m === void 0 ? void 0 : _m.fetch,
                logging: this._options.logging,
            });
            if (_response.ok) {
                return {
                    data: serializers.hris.Benefit.parseOrThrow(_response.body, {
                        unrecognizedObjectKeys: "passthrough",
                        allowUnrecognizedUnionMembers: true,
                        allowUnrecognizedEnumValues: true,
                        skipValidation: true,
                        breadcrumbsPrefix: ["response"],
                    }),
                    rawResponse: _response.rawResponse,
                };
            }
            if (_response.error.reason === "status-code") {
                throw new errors.MergeError({
                    statusCode: _response.error.statusCode,
                    body: _response.error.body,
                    rawResponse: _response.rawResponse,
                });
            }
            return (0, handleNonStatusCodeError_1.handleNonStatusCodeError)(_response.error, _response.rawResponse, "GET", "/hris/v1/benefits/{id}");
        });
    }
}
exports.BenefitsClient = BenefitsClient;
