"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.Scopes = void 0;
const environments = __importStar(require("../../../../../../environments"));
const core = __importStar(require("../../../../../../core"));
const headers_1 = require("../../../../../../core/headers");
const serializers = __importStar(require("../../../../../../serialization/index"));
const errors = __importStar(require("../../../../../../errors/index"));
class Scopes {
    constructor(_options) {
        this._options = _options;
    }
    /**
     * Get the default permissions for Merge Common Models and fields across all Linked Accounts of a given category. [Learn more](https://help.merge.dev/en/articles/5950052-common-model-and-field-scopes).
     *
     * @param {Scopes.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.ticketing.scopes.defaultScopesRetrieve()
     */
    defaultScopesRetrieve(requestOptions) {
        return core.HttpResponsePromise.fromPromise(this.__defaultScopesRetrieve(requestOptions));
    }
    __defaultScopesRetrieve(requestOptions) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b, _c, _d;
            const _response = yield ((_a = this._options.fetcher) !== null && _a !== void 0 ? _a : core.fetcher)({
                url: core.url.join((_c = (_b = (yield core.Supplier.get(this._options.baseUrl))) !== null && _b !== void 0 ? _b : (yield core.Supplier.get(this._options.environment))) !== null && _c !== void 0 ? _c : environments.MergeEnvironment.Production, "ticketing/v1/default-scopes"),
                method: "GET",
                headers: (0, headers_1.mergeHeaders)((_d = this._options) === null || _d === void 0 ? void 0 : _d.headers, (0, headers_1.mergeOnlyDefinedHeaders)({
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Account-Token": requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.accountToken,
                }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers),
                queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams,
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
                abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
            });
            if (_response.ok) {
                return {
                    data: serializers.ticketing.CommonModelScopeApi.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,
                });
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.MergeError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                        rawResponse: _response.rawResponse,
                    });
                case "timeout":
                    throw new errors.MergeTimeoutError("Timeout exceeded when calling GET /ticketing/v1/default-scopes.");
                case "unknown":
                    throw new errors.MergeError({
                        message: _response.error.errorMessage,
                        rawResponse: _response.rawResponse,
                    });
            }
        });
    }
    /**
     * Get all available permissions for Merge Common Models and fields for a single Linked Account. [Learn more](https://help.merge.dev/en/articles/5950052-common-model-and-field-scopes).
     *
     * @param {Scopes.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.ticketing.scopes.linkedAccountScopesRetrieve()
     */
    linkedAccountScopesRetrieve(requestOptions) {
        return core.HttpResponsePromise.fromPromise(this.__linkedAccountScopesRetrieve(requestOptions));
    }
    __linkedAccountScopesRetrieve(requestOptions) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b, _c, _d;
            const _response = yield ((_a = this._options.fetcher) !== null && _a !== void 0 ? _a : core.fetcher)({
                url: core.url.join((_c = (_b = (yield core.Supplier.get(this._options.baseUrl))) !== null && _b !== void 0 ? _b : (yield core.Supplier.get(this._options.environment))) !== null && _c !== void 0 ? _c : environments.MergeEnvironment.Production, "ticketing/v1/linked-account-scopes"),
                method: "GET",
                headers: (0, headers_1.mergeHeaders)((_d = this._options) === null || _d === void 0 ? void 0 : _d.headers, (0, headers_1.mergeOnlyDefinedHeaders)({
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Account-Token": requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.accountToken,
                }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers),
                queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams,
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
                abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
            });
            if (_response.ok) {
                return {
                    data: serializers.ticketing.CommonModelScopeApi.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,
                });
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.MergeError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                        rawResponse: _response.rawResponse,
                    });
                case "timeout":
                    throw new errors.MergeTimeoutError("Timeout exceeded when calling GET /ticketing/v1/linked-account-scopes.");
                case "unknown":
                    throw new errors.MergeError({
                        message: _response.error.errorMessage,
                        rawResponse: _response.rawResponse,
                    });
            }
        });
    }
    /**
     * Update permissions for any Common Model or field for a single Linked Account. Any Scopes not set in this POST request will inherit the default Scopes. [Learn more](https://help.merge.dev/en/articles/5950052-common-model-and-field-scopes)
     *
     * @param {Merge.ticketing.LinkedAccountCommonModelScopeDeserializerRequest} request
     * @param {Scopes.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.ticketing.scopes.linkedAccountScopesCreate({
     *         commonModels: [{
     *                 modelName: "Employee",
     *                 modelPermissions: {
     *                     "READ": {
     *                         isEnabled: true
     *                     },
     *                     "WRITE": {
     *                         isEnabled: false
     *                     }
     *                 },
     *                 fieldPermissions: {
     *                     enabledFields: ["avatar", "home_location"],
     *                     disabledFields: ["work_location"]
     *                 }
     *             }, {
     *                 modelName: "Benefit",
     *                 modelPermissions: {
     *                     "WRITE": {
     *                         isEnabled: false
     *                     }
     *                 }
     *             }]
     *     })
     */
    linkedAccountScopesCreate(request, requestOptions) {
        return core.HttpResponsePromise.fromPromise(this.__linkedAccountScopesCreate(request, requestOptions));
    }
    __linkedAccountScopesCreate(request, requestOptions) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b, _c, _d;
            const _response = yield ((_a = this._options.fetcher) !== null && _a !== void 0 ? _a : core.fetcher)({
                url: core.url.join((_c = (_b = (yield core.Supplier.get(this._options.baseUrl))) !== null && _b !== void 0 ? _b : (yield core.Supplier.get(this._options.environment))) !== null && _c !== void 0 ? _c : environments.MergeEnvironment.Production, "ticketing/v1/linked-account-scopes"),
                method: "POST",
                headers: (0, headers_1.mergeHeaders)((_d = this._options) === null || _d === void 0 ? void 0 : _d.headers, (0, headers_1.mergeOnlyDefinedHeaders)({
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Account-Token": requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.accountToken,
                }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers),
                contentType: "application/json",
                queryParameters: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.queryParams,
                requestType: "json",
                body: serializers.ticketing.LinkedAccountCommonModelScopeDeserializerRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "strip",
                }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
                abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
            });
            if (_response.ok) {
                return {
                    data: serializers.ticketing.CommonModelScopeApi.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,
                });
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.MergeError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                        rawResponse: _response.rawResponse,
                    });
                case "timeout":
                    throw new errors.MergeTimeoutError("Timeout exceeded when calling POST /ticketing/v1/linked-account-scopes.");
                case "unknown":
                    throw new errors.MergeError({
                        message: _response.error.errorMessage,
                        rawResponse: _response.rawResponse,
                    });
            }
        });
    }
    _getAuthorizationHeader() {
        return __awaiter(this, void 0, void 0, function* () {
            return `Bearer ${yield core.Supplier.get(this._options.apiKey)}`;
        });
    }
}
exports.Scopes = Scopes;
