import "../../common/atomic-icon/atomic-icon.js";
import { NumberValue, Schema } from "@coveo/bueno";
import { buildNotifyTrigger } from "@coveo/headless";
import { LitElement, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { map } from "lit/directives/map.js";
import { renderHeading } from "../../common/heading.js";
import { ValidatePropsController } from "../../common/validate-props-controller/validate-props-controller.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 { InitializeBindingsMixin } from "../../../mixins/bindings-mixin.js";
import { AriaLiveRegionController } from "../../../utils/accessibility-utils.js";
import info from "../../../images/info.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 AtomicNotifications extends InitializeBindingsMixin(LitElement) {
    initialize() {
        this.notifyTrigger = buildNotifyTrigger(this.bindings.engine);
    }
    get notifications() {
        return this.notifyTriggerState?.notifications || [];
    }
    generateAriaMessage() {
        if (1 === this.notifications.length) return this.notifications[0];
        return this.notifications.map((text, i)=>this.bindings.i18n.t('notification-n', {
                n: i + 1,
                text
            })).join('\n');
    }
    renderNotification(text) {
        return html`
      <div
        part="notification"
        class="bg-background border-neutral-dark flex items-center rounded-md border p-6 shadow-lg"
      >
        <atomic-icon
          icon=${this.icon ?? info}
          part="icon"
          class="text-neutral-dark mr-6 h-7 w-7"
        ></atomic-icon>
        <span part="text" class="text-on-background text-base leading-5">
          ${text}
        </span>
      </div>
    `;
    }
    renderNotifications() {
        return html`
      <div part="notifications" class="flex flex-col gap-4">
        ${map(this.notifications, (text)=>this.renderNotification(text))}
      </div>
    `;
    }
    render() {
        if (!this.notifications.length) return nothing;
        this.ariaMessage.message = this.generateAriaMessage();
        return html`
      ${renderHeading({
            props: {
                level: this.headingLevel,
                class: 'sr-only'
            }
        })(html`${this.bindings.i18n.t('notifications')}`)}
      ${this.renderNotifications()}
    `;
    }
    constructor(){
        super(), this.headingLevel = 0, this.ariaMessage = new AriaLiveRegionController(this, 'notifications');
        new ValidatePropsController(this, ()=>({
                headingLevel: this.headingLevel
            }), AtomicNotifications.schema, false);
    }
}
AtomicNotifications.schema = new Schema({
    headingLevel: new NumberValue({
        min: 0,
        max: 6,
        required: false
    })
});
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicNotifications.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicNotifications.prototype, "error", void 0);
_ts_decorate([
    property({
        type: Number,
        reflect: true,
        attribute: 'heading-level'
    })
], AtomicNotifications.prototype, "headingLevel", void 0);
_ts_decorate([
    property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicNotifications.prototype, "icon", void 0);
_ts_decorate([
    bindStateToController('notifyTrigger'),
    state(),
    _ts_metadata("design:type", "u" < typeof NotifyTriggerState ? Object : NotifyTriggerState)
], AtomicNotifications.prototype, "notifyTriggerState", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicNotifications.prototype, "render", null);
AtomicNotifications = _ts_decorate([
    customElement('atomic-notifications'),
    bindings(),
    withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicNotifications);
export { AtomicNotifications };
