import { __esDecorate, __runInitializers } from "tslib";
import { html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js';
import { EventTypes } from '../../events/amf/EventTypes.js';
import ApiElement from '../ApiElement.js';
import '../../define/amf/api-request-editor.js';
import '../../define/http/http-request-log.js';
let ApiRequest = (() => {
    let _classSuper = ApiElement;
    let _instanceExtraInitializers = [];
    let _handleNavigationEvents_decorators;
    let _handleNavigationEvents_initializers = [];
    let _handleNavigationEvents_extraInitializers = [];
    let _urlEditor_decorators;
    let _urlEditor_initializers = [];
    let _urlEditor_extraInitializers = [];
    let _urlLabel_decorators;
    let _urlLabel_initializers = [];
    let _urlLabel_extraInitializers = [];
    let _baseUri_decorators;
    let _baseUri_initializers = [];
    let _baseUri_extraInitializers = [];
    let _redirectUri_decorators;
    let _redirectUri_initializers = [];
    let _redirectUri_extraInitializers = [];
    let _allowHideOptional_decorators;
    let _allowHideOptional_initializers = [];
    let _allowHideOptional_extraInitializers = [];
    let _allowCustom_decorators;
    let _allowCustom_initializers = [];
    let _allowCustom_extraInitializers = [];
    let _serverValue_decorators;
    let _serverValue_initializers = [];
    let _serverValue_extraInitializers = [];
    let _serverType_decorators;
    let _serverType_initializers = [];
    let _serverType_extraInitializers = [];
    let _noServerSelector_decorators;
    let _noServerSelector_initializers = [];
    let _noServerSelector_extraInitializers = [];
    let _allowCustomBaseUri_decorators;
    let _allowCustomBaseUri_initializers = [];
    let _allowCustomBaseUri_extraInitializers = [];
    let _credentialsSource_decorators;
    let _credentialsSource_initializers = [];
    let _credentialsSource_extraInitializers = [];
    let _set_domainId_decorators;
    let _response_decorators;
    let _response_initializers = [];
    let _response_extraInitializers = [];
    return class ApiRequest extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _handleNavigationEvents_decorators = [property({ type: Boolean, reflect: true })];
            _urlEditor_decorators = [property({ type: Boolean, reflect: true })];
            _urlLabel_decorators = [property({ type: Boolean, reflect: true })];
            _baseUri_decorators = [property({ type: String })];
            _redirectUri_decorators = [property({ type: String })];
            _allowHideOptional_decorators = [property({ type: Boolean, reflect: true })];
            _allowCustom_decorators = [property({ type: Boolean, reflect: true })];
            _serverValue_decorators = [property({ type: String })];
            _serverType_decorators = [property({ type: String })];
            _noServerSelector_decorators = [property({ type: Boolean, reflect: true })];
            _allowCustomBaseUri_decorators = [property({ type: Boolean, reflect: true })];
            _credentialsSource_decorators = [property({ type: Array })];
            _set_domainId_decorators = [property({ type: String })];
            _response_decorators = [state()];
            __esDecorate(this, null, _handleNavigationEvents_decorators, { kind: "accessor", name: "handleNavigationEvents", static: false, private: false, access: { has: obj => "handleNavigationEvents" in obj, get: obj => obj.handleNavigationEvents, set: (obj, value) => { obj.handleNavigationEvents = value; } }, metadata: _metadata }, _handleNavigationEvents_initializers, _handleNavigationEvents_extraInitializers);
            __esDecorate(this, null, _urlEditor_decorators, { kind: "accessor", name: "urlEditor", static: false, private: false, access: { has: obj => "urlEditor" in obj, get: obj => obj.urlEditor, set: (obj, value) => { obj.urlEditor = value; } }, metadata: _metadata }, _urlEditor_initializers, _urlEditor_extraInitializers);
            __esDecorate(this, null, _urlLabel_decorators, { kind: "accessor", name: "urlLabel", static: false, private: false, access: { has: obj => "urlLabel" in obj, get: obj => obj.urlLabel, set: (obj, value) => { obj.urlLabel = value; } }, metadata: _metadata }, _urlLabel_initializers, _urlLabel_extraInitializers);
            __esDecorate(this, null, _baseUri_decorators, { kind: "accessor", name: "baseUri", static: false, private: false, access: { has: obj => "baseUri" in obj, get: obj => obj.baseUri, set: (obj, value) => { obj.baseUri = value; } }, metadata: _metadata }, _baseUri_initializers, _baseUri_extraInitializers);
            __esDecorate(this, null, _redirectUri_decorators, { kind: "accessor", name: "redirectUri", static: false, private: false, access: { has: obj => "redirectUri" in obj, get: obj => obj.redirectUri, set: (obj, value) => { obj.redirectUri = value; } }, metadata: _metadata }, _redirectUri_initializers, _redirectUri_extraInitializers);
            __esDecorate(this, null, _allowHideOptional_decorators, { kind: "accessor", name: "allowHideOptional", static: false, private: false, access: { has: obj => "allowHideOptional" in obj, get: obj => obj.allowHideOptional, set: (obj, value) => { obj.allowHideOptional = value; } }, metadata: _metadata }, _allowHideOptional_initializers, _allowHideOptional_extraInitializers);
            __esDecorate(this, null, _allowCustom_decorators, { kind: "accessor", name: "allowCustom", static: false, private: false, access: { has: obj => "allowCustom" in obj, get: obj => obj.allowCustom, set: (obj, value) => { obj.allowCustom = value; } }, metadata: _metadata }, _allowCustom_initializers, _allowCustom_extraInitializers);
            __esDecorate(this, null, _serverValue_decorators, { kind: "accessor", name: "serverValue", static: false, private: false, access: { has: obj => "serverValue" in obj, get: obj => obj.serverValue, set: (obj, value) => { obj.serverValue = value; } }, metadata: _metadata }, _serverValue_initializers, _serverValue_extraInitializers);
            __esDecorate(this, null, _serverType_decorators, { kind: "accessor", name: "serverType", static: false, private: false, access: { has: obj => "serverType" in obj, get: obj => obj.serverType, set: (obj, value) => { obj.serverType = value; } }, metadata: _metadata }, _serverType_initializers, _serverType_extraInitializers);
            __esDecorate(this, null, _noServerSelector_decorators, { kind: "accessor", name: "noServerSelector", static: false, private: false, access: { has: obj => "noServerSelector" in obj, get: obj => obj.noServerSelector, set: (obj, value) => { obj.noServerSelector = value; } }, metadata: _metadata }, _noServerSelector_initializers, _noServerSelector_extraInitializers);
            __esDecorate(this, null, _allowCustomBaseUri_decorators, { kind: "accessor", name: "allowCustomBaseUri", static: false, private: false, access: { has: obj => "allowCustomBaseUri" in obj, get: obj => obj.allowCustomBaseUri, set: (obj, value) => { obj.allowCustomBaseUri = value; } }, metadata: _metadata }, _allowCustomBaseUri_initializers, _allowCustomBaseUri_extraInitializers);
            __esDecorate(this, null, _credentialsSource_decorators, { kind: "accessor", name: "credentialsSource", static: false, private: false, access: { has: obj => "credentialsSource" in obj, get: obj => obj.credentialsSource, set: (obj, value) => { obj.credentialsSource = value; } }, metadata: _metadata }, _credentialsSource_initializers, _credentialsSource_extraInitializers);
            __esDecorate(this, null, _set_domainId_decorators, { kind: "setter", name: "domainId", static: false, private: false, access: { has: obj => "domainId" in obj, set: (obj, value) => { obj.domainId = value; } }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(this, null, _response_decorators, { kind: "accessor", name: "response", static: false, private: false, access: { has: obj => "response" in obj, get: obj => obj.response, set: (obj, value) => { obj.response = value; } }, metadata: _metadata }, _response_initializers, _response_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        #handleNavigationEvents_accessor_storage = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _handleNavigationEvents_initializers, void 0));
        /**
         * By default application hosting the element must set `domainId`
         * property. When using `api-navigation` element
         * by setting this property the element listens for navigation events
         * and updates the state
         * @attribute
         */
        get handleNavigationEvents() { return this.#handleNavigationEvents_accessor_storage; }
        set handleNavigationEvents(value) { this.#handleNavigationEvents_accessor_storage = value; }
        #urlEditor_accessor_storage = (__runInitializers(this, _handleNavigationEvents_extraInitializers), __runInitializers(this, _urlEditor_initializers, void 0));
        /**
         * When set it renders the URL input above the URL parameters.
         * @attribute
         */
        get urlEditor() { return this.#urlEditor_accessor_storage; }
        set urlEditor(value) { this.#urlEditor_accessor_storage = value; }
        #urlLabel_accessor_storage = (__runInitializers(this, _urlEditor_extraInitializers), __runInitializers(this, _urlLabel_initializers, void 0));
        /**
         * When set it renders a label with the computed URL.
         * @attribute
         */
        get urlLabel() { return this.#urlLabel_accessor_storage; }
        set urlLabel(value) { this.#urlLabel_accessor_storage = value; }
        #baseUri_accessor_storage = (__runInitializers(this, _urlLabel_extraInitializers), __runInitializers(this, _baseUri_initializers, void 0));
        /**
         * A base URI for the API. To be set if RAML spec is missing `baseUri`
         * declaration and this produces invalid URL input. This information
         * is passed to the URL editor that prefixes the URL with `baseUri` value
         * if passed URL is a relative URL.
         * @attribute
         */
        get baseUri() { return this.#baseUri_accessor_storage; }
        set baseUri(value) { this.#baseUri_accessor_storage = value; }
        #redirectUri_accessor_storage = (__runInitializers(this, _baseUri_extraInitializers), __runInitializers(this, _redirectUri_initializers, void 0));
        /**
         * OAuth2 redirect URI.
         * This value **must** be set in order for OAuth 1/2 to work properly.
         * @attribute
         */
        get redirectUri() { return this.#redirectUri_accessor_storage; }
        set redirectUri(value) { this.#redirectUri_accessor_storage = value; }
        #allowHideOptional_accessor_storage = (__runInitializers(this, _redirectUri_extraInitializers), __runInitializers(this, _allowHideOptional_initializers, void 0));
        /**
         * If set it computes `hasOptional` property and shows checkbox in the
         * form to show / hide optional properties.
         * @attribute
         */
        get allowHideOptional() { return this.#allowHideOptional_accessor_storage; }
        set allowHideOptional(value) { this.#allowHideOptional_accessor_storage = value; }
        #allowCustom_accessor_storage = (__runInitializers(this, _allowHideOptional_extraInitializers), __runInitializers(this, _allowCustom_initializers, void 0));
        /**
         * When set, renders "add custom" item button.
         * If the element is to be used without AMF model this should always
         * be enabled. Otherwise users won't be able to add a parameter.
         * @attribute
         */
        get allowCustom() { return this.#allowCustom_accessor_storage; }
        set allowCustom(value) { this.#allowCustom_accessor_storage = value; }
        #serverValue_accessor_storage = (__runInitializers(this, _allowCustom_extraInitializers), __runInitializers(this, _serverValue_initializers, void 0));
        /**
         * Holds the value of the currently selected server
         * Data type: URI
         * @attribute
         */
        get serverValue() { return this.#serverValue_accessor_storage; }
        set serverValue(value) { this.#serverValue_accessor_storage = value; }
        #serverType_accessor_storage = (__runInitializers(this, _serverValue_extraInitializers), __runInitializers(this, _serverType_initializers, void 0));
        /**
         * Holds the type of the currently selected server
         * Values: `server` | `slot` | `custom`
         * @attribute
         */
        get serverType() { return this.#serverType_accessor_storage; }
        set serverType(value) { this.#serverType_accessor_storage = value; }
        #noServerSelector_accessor_storage = (__runInitializers(this, _serverType_extraInitializers), __runInitializers(this, _noServerSelector_initializers, void 0));
        /**
         * Optional property to set
         * If true, the server selector is not rendered
         * @attribute
         */
        get noServerSelector() { return this.#noServerSelector_accessor_storage; }
        set noServerSelector(value) { this.#noServerSelector_accessor_storage = value; }
        #allowCustomBaseUri_accessor_storage = (__runInitializers(this, _noServerSelector_extraInitializers), __runInitializers(this, _allowCustomBaseUri_initializers, void 0));
        /**
         * Optional property to set
         * If true, the server selector custom base URI option is rendered
         * @attribute
         */
        get allowCustomBaseUri() { return this.#allowCustomBaseUri_accessor_storage; }
        set allowCustomBaseUri(value) { this.#allowCustomBaseUri_accessor_storage = value; }
        #credentialsSource_accessor_storage = (__runInitializers(this, _allowCustomBaseUri_extraInitializers), __runInitializers(this, _credentialsSource_initializers, void 0));
        /**
         * List of credentials source
         */
        get credentialsSource() { return this.#credentialsSource_accessor_storage; }
        set credentialsSource(value) { this.#credentialsSource_accessor_storage = value; }
        domainIdValue = __runInitializers(this, _credentialsSource_extraInitializers);
        /**
         * The domain id (AMF's id) of an API operation.
         */
        get domainId() {
            return this.domainIdValue;
        }
        set domainId(value) {
            const old = this.domainIdValue;
            /* istanbul ignore if */
            if (old === value) {
                return;
            }
            this.domainIdValue = value;
            this.requestUpdate('domainId', old);
            this.domainIdChanged(value);
        }
        #response_accessor_storage = __runInitializers(this, _response_initializers, void 0);
        /**
         * The latest response for the current request.
         */
        get response() { return this.#response_accessor_storage; }
        set response(value) { this.#response_accessor_storage = value; }
        constructor() {
            super();
            __runInitializers(this, _response_extraInitializers);
            this.navigationHandler = this.navigationHandler.bind(this);
            this.handleNavigationEvents = false;
            this.allowHideOptional = false;
            this.allowCustom = false;
            this.noServerSelector = false;
            this.allowCustomBaseUri = false;
        }
        connectedCallback() {
            super.connectedCallback();
            const node = this.eventsTarget;
            node.addEventListener(EventTypes.Navigation.apiNavigate, this.navigationHandler);
        }
        disconnectedCallback() {
            super.disconnectedCallback();
            const node = this.eventsTarget;
            node.removeEventListener(EventTypes.Navigation.apiNavigate, this.navigationHandler);
        }
        /**
         * Serializes the state of the request editor.
         *
         * Note this is the same object as the one passed to the detail of the api request event.
         */
        async serialize() {
            const { shadowRoot } = this;
            if (!shadowRoot) {
                throw new Error(`The component is not yet initialized.`);
            }
            const editor = shadowRoot.querySelector('api-request-editor');
            if (!editor) {
                throw new Error(`The request editor has been manually removed from the DOM.`);
            }
            return editor.serialize();
        }
        /**
         * Clears response panel when the `domainId` change.
         */
        domainIdChanged(id) {
            if (!id) {
                return;
            }
            this.clearResponse();
        }
        /**
         * Clears response panel.
         */
        clearResponse() {
            if (this.response) {
                this.response = undefined;
            }
        }
        /**
         * Handles navigation events and computes available servers.
         *
         * When `handleNavigationEvents` is set then it also manages the selection.
         */
        navigationHandler(e) {
            if (this.handleNavigationEvents) {
                const { domainId, domainType } = e.detail;
                this.domainId = domainType === 'operation' ? domainId : undefined;
            }
        }
        /**
         * Retargets the change event from the editor.
         */
        changeHandler() {
            this.notifyChange();
        }
        handleResponse(e) {
            this.response = e.detail;
        }
        render() {
            return html `${this.requestTemplate()}${this.responseTemplate()}`;
        }
        /**
         * @returns A template for the request panel
         */
        requestTemplate() {
            const { redirectUri, domainId, urlEditor, urlLabel, baseUri, eventsTarget, allowHideOptional, allowCustom, serverValue, serverType, noServerSelector, allowCustomBaseUri, credentialsSource, } = this;
            return html ` <api-request-editor
      .redirectUri="${redirectUri}"
      .domainId="${domainId}"
      ?urlEditor="${urlEditor}"
      ?urlLabel="${urlLabel}"
      .baseUri="${baseUri}"
      .eventsTarget="${eventsTarget}"
      ?allowHideOptional="${allowHideOptional}"
      ?allowCustom="${allowCustom}"
      .serverValue="${serverValue}"
      .serverType="${serverType}"
      ?noServerSelector="${noServerSelector}"
      ?allowCustomBaseUri="${allowCustomBaseUri}"
      .credentialsSource="${credentialsSource}"
      @change="${this.changeHandler}"
      @response="${this.handleResponse}"
    >
      <slot name="custom-base-uri" slot="custom-base-uri"></slot>
    </api-request-editor>`;
        }
        /**
         * @returns A template for the response view
         */
        responseTemplate() {
            const { response } = this;
            if (!response) {
                return nothing;
            }
            return html `<http-request-log .httpLog="${response}"></http-request-log>`;
        }
    };
})();
export default ApiRequest;
//# sourceMappingURL=ApiRequest.element.js.map