import { provide } from "@lit/context";
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { bindingsContext } from "../../common/context/bindings-context.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { ChildrenUpdateCompleteMixin } from "../../../mixins/children-update-complete-mixin.js";
import { buildCustomEvent } from "../../../utils/event-utils.js";
import { isParentReady, markParentAsReady } from "../../../utils/init-queue.js";
import { initializeEventName } from "../../../utils/initialization-common-utils.js";
function _check_private_redeclaration(obj, privateCollection) {
    if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
}
function _class_apply_descriptor_get(receiver, descriptor) {
    if (descriptor.get) return descriptor.get.call(receiver);
    return descriptor.value;
}
function _class_extract_field_descriptor(receiver, privateMap, action) {
    if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance");
    return privateMap.get(receiver);
}
function _class_private_field_get(receiver, privateMap) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
    return _class_apply_descriptor_get(receiver, descriptor);
}
function _class_private_field_init(obj, privateMap, value) {
    _check_private_redeclaration(obj, privateMap);
    privateMap.set(obj, value);
}
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);
}
var _interface = /*#__PURE__*/ new WeakMap();
class AtomicExternal extends ChildrenUpdateCompleteMixin(LitElement) {
    async connectedCallback() {
        super.connectedCallback();
        await this.getUpdateComplete();
        this.addEventListener('atomic/initializeComponent', this.handleInitialization);
        this.addEventListener('atomic/scrollToTop', this.handleScrollToTop);
        _class_private_field_get(this, _interface)?.addEventListener('atomic/parentReady', this.handleParentReady);
        if (isParentReady(_class_private_field_get(this, _interface))) markParentAsReady(this);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.removeEventListener('atomic/initializeComponent', this.handleInitialization);
        this.removeEventListener('atomic/scrollToTop', this.handleScrollToTop);
        _class_private_field_get(this, _interface)?.removeEventListener('atomic/parentReady', this.handleParentReady);
    }
    render() {
        return html`<slot></slot>`;
    }
    constructor(...args){
        super(...args), _class_private_field_init(this, _interface, {
            get: get_interface,
            set: void 0
        }), this.selector = 'atomic-search-interface', this.bindings = {}, this.handleInitialization = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            _class_private_field_get(this, _interface)?.dispatchEvent(buildCustomEvent(initializeEventName, event.detail));
        }, this.handleScrollToTop = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            _class_private_field_get(this, _interface)?.dispatchEvent(buildCustomEvent('atomic/scrollToTop', event.detail));
        }, this.handleParentReady = (event)=>{
            if (event.target === this.boundInterface) {
                this.bindings = _class_private_field_get(this, _interface)?.bindings;
                markParentAsReady(this);
            }
        };
    }
}
function get_interface() {
    if (!this.boundInterface) {
        this.boundInterface = document.querySelector(this.selector) ?? void 0;
        if (!this.boundInterface) {
            const error = new Error(`Cannot find interface element with selector "${this.selector}"`);
            this.error = error;
            return;
        }
    }
    return this.boundInterface;
}
_ts_decorate([
    property({
        type: String,
        reflect: true
    })
], AtomicExternal.prototype, "selector", void 0);
_ts_decorate([
    property({
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof AtomicInterface ? Object : AtomicInterface)
], AtomicExternal.prototype, "boundInterface", void 0);
_ts_decorate([
    state(),
    provide({
        context: bindingsContext
    }),
    _ts_metadata("design:type", "u" < typeof AnyBindings ? Object : AnyBindings)
], AtomicExternal.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicExternal.prototype, "error", void 0);
_ts_decorate([
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicExternal.prototype, "render", null);
AtomicExternal = _ts_decorate([
    customElement('atomic-external')
], AtomicExternal);
export { AtomicExternal };
