import { __esDecorate, __runInitializers } from "tslib";
import { property } from 'lit/decorators.js';
import { EventTypes as CoreEventTypes } from '@api-client/core/events/EventTypes.js';
import { OidcAuthorization } from '@api-client/core/authorization/OidcAuthorization.js';
import ApiElement from '../ApiElement.js';
export const authorizeHandler = Symbol('authorizeHandler');
let OidcAuthorize = (() => {
    let _classSuper = ApiElement;
    let _tokenProxy_decorators;
    let _tokenProxy_initializers = [];
    let _tokenProxy_extraInitializers = [];
    let _tokenProxyEncode_decorators;
    let _tokenProxyEncode_initializers = [];
    let _tokenProxyEncode_extraInitializers = [];
    return class OidcAuthorize extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            __esDecorate(this, null, _tokenProxy_decorators, { kind: "accessor", name: "tokenProxy", static: false, private: false, access: { has: obj => "tokenProxy" in obj, get: obj => obj.tokenProxy, set: (obj, value) => { obj.tokenProxy = value; } }, metadata: _metadata }, _tokenProxy_initializers, _tokenProxy_extraInitializers);
            __esDecorate(this, null, _tokenProxyEncode_decorators, { kind: "accessor", name: "tokenProxyEncode", static: false, private: false, access: { has: obj => "tokenProxyEncode" in obj, get: obj => obj.tokenProxyEncode, set: (obj, value) => { obj.tokenProxyEncode = value; } }, metadata: _metadata }, _tokenProxyEncode_initializers, _tokenProxyEncode_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #tokenProxy_accessor_storage = __runInitializers(this, _tokenProxy_initializers, void 0);
        /**
         * When set it uses this value to prefix the call to the
         * OAuth 2 token endpoint. This is to support use cases when
         * the requests should be proxied through a server to avoid CORS problems.
         * @attribute
         */
        get tokenProxy() { return this.#tokenProxy_accessor_storage; }
        set tokenProxy(value) { this.#tokenProxy_accessor_storage = value; }
        #tokenProxyEncode_accessor_storage = (__runInitializers(this, _tokenProxy_extraInitializers), __runInitializers(this, _tokenProxyEncode_initializers, void 0));
        /**
         * When set it encodes the token URI value before adding it to the
         * `tokenProxy`. This is to be used when the proxy takes the target
         * URL as a query parameter.
         * @attribute
         */
        get tokenProxyEncode() { return this.#tokenProxyEncode_accessor_storage; }
        set tokenProxyEncode(value) { this.#tokenProxyEncode_accessor_storage = value; }
        constructor() {
            super();
            __runInitializers(this, _tokenProxyEncode_extraInitializers);
            this[authorizeHandler] = this[authorizeHandler].bind(this);
        }
        connectedCallback() {
            super.connectedCallback();
            this.eventsTarget.addEventListener(CoreEventTypes.Authorization.Oidc.authorize, this[authorizeHandler]);
            this.setAttribute('aria-hidden', 'true');
        }
        disconnectedCallback() {
            super.disconnectedCallback();
            this.eventsTarget.removeEventListener(CoreEventTypes.Authorization.Oidc.authorize, this[authorizeHandler]);
        }
        [(_tokenProxy_decorators = [property({ type: String })], _tokenProxyEncode_decorators = [property({ type: String })], authorizeHandler)](event) {
            const e = event;
            const config = { ...e.detail };
            e.detail.result = this.authorize(config);
        }
        /**
         * Authorize the user using provided settings.
         * This is left for compatibility. Use the `OAuth2Authorization` instead.
         *
         * @param settings The authorization configuration.
         */
        async authorize(settings) {
            const { tokenProxy, tokenProxyEncode } = this;
            const options = {};
            if (tokenProxy && typeof tokenProxy === 'string') {
                options.tokenProxy = tokenProxy;
            }
            if (tokenProxy && tokenProxyEncode && typeof tokenProxyEncode === 'boolean') {
                options.tokenProxyEncode = tokenProxyEncode;
            }
            const auth = new OidcAuthorization(settings, options);
            auth.checkConfig();
            // FIXME: Fix the typings in the `OidcAuthorization` class.
            return auth.authorize();
        }
    };
})();
export default OidcAuthorize;
//# sourceMappingURL=OidcAuthorize.js.map