import { buildQueryError, getOrganizationEndpoint } from "@coveo/headless/insight";
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { renderQueryErrorContainer } from "../../common/query-error/container.js";
import { renderQueryErrorDescription } from "../../common/query-error/description.js";
import { renderQueryErrorDetails } from "../../common/query-error/details.js";
import { renderQueryErrorIcon } from "../../common/query-error/icon.js";
import { renderQueryErrorLink } from "../../common/query-error/link.js";
import { renderQueryErrorShowMore } from "../../common/query-error/show-more.js";
import { renderQueryErrorTitle } from "../../common/query-error/title.js";
import { getAriaMessageFromErrorType } from "../../common/query-error/utils.js";
import { bindStateToController } from "../../../decorators/bind-state.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { AriaLiveRegionController } from "../../../utils/accessibility-utils.js";
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
class AtomicInsightQueryError extends LitElement {
    initialize() {
        this.queryError = buildQueryError(this.bindings.engine);
    }
    render() {
        const { hasError, error } = this.queryErrorState;
        const i18n = this.bindings.i18n;
        const { organizationId, environment } = this.bindings.engine.state.configuration;
        const url = getOrganizationEndpoint(organizationId, environment);
        if (hasError) this.ariaMessage.message = getAriaMessageFromErrorType(i18n, organizationId, url, error?.type);
        const linkResult = renderQueryErrorLink({
            props: {
                i18n,
                errorType: error?.type
            }
        });
        return html`${when(hasError, ()=>html`
        ${renderQueryErrorContainer()(html`
          ${renderQueryErrorIcon({
                props: {
                    errorType: error?.type
                }
            })}
          ${renderQueryErrorTitle({
                props: {
                    errorType: error?.type,
                    i18n,
                    organizationId
                }
            })}
          ${renderQueryErrorDescription({
                props: {
                    i18n,
                    organizationId,
                    url,
                    errorType: error?.type
                }
            })}
          ${renderQueryErrorShowMore({
                props: {
                    link: linkResult,
                    onShowMore: ()=>{
                        this.showMoreInfo = !this.showMoreInfo;
                    },
                    i18n
                }
            })}
          ${renderQueryErrorDetails({
                props: {
                    error,
                    show: this.showMoreInfo
                }
            })}
        `)}
      `)}`;
    }
    constructor(...args){
        super(...args), this.ariaMessage = new AriaLiveRegionController(this, 'query-error'), this.showMoreInfo = false;
    }
}
_ts_decorate([
    bindStateToController('queryError'),
    state(),
    _ts_metadata("design:type", "u" < typeof QueryErrorState ? Object : QueryErrorState)
], AtomicInsightQueryError.prototype, "queryErrorState", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightQueryError.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightQueryError.prototype, "error", void 0);
_ts_decorate([
    state()
], AtomicInsightQueryError.prototype, "showMoreInfo", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightQueryError.prototype, "render", null);
AtomicInsightQueryError = _ts_decorate([
    customElement('atomic-insight-query-error'),
    bindings(),
    withTailwindStyles
], AtomicInsightQueryError);
export { AtomicInsightQueryError };
