import { buildAttachedResults } from "@coveo/headless/insight";
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { renderIconButton } from "../../common/icon-button.js";
import { ItemContextController } from "../../common/item-list/context/item-context-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 { LightDomMixin } from "../../../mixins/light-dom.js";
import attach from "../../../images/attach.js";
import detach from "../../../images/detach.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 AtomicInsightResultAttachToCaseAction extends LightDomMixin(LitElement) {
    get result() {
        return this.itemContextController.item;
    }
    initialize() {
        const caseId = this.bindings.engine.state.insightCaseContext?.caseId || '';
        this.attachedResults = buildAttachedResults(this.bindings.engine, {
            options: {
                caseId: caseId
            }
        });
    }
    onClick() {
        if (!this.result) return;
        if (this.attachedResults.isAttached(this.result)) this.dispatchEvent(new CustomEvent('atomic/insight/attachToCase/detach', {
            bubbles: true,
            composed: true,
            cancelable: true,
            detail: {
                callback: ()=>this.attachedResults.detach(this.result),
                result: this.result
            }
        }));
        else this.dispatchEvent(new CustomEvent('atomic/insight/attachToCase/attach', {
            bubbles: true,
            composed: true,
            cancelable: true,
            detail: {
                callback: ()=>this.attachedResults.attach(this.result),
                result: this.result
            }
        }));
    }
    getIcon() {
        return this.result && this.attachedResults?.isAttached(this.result) ? detach : attach;
    }
    getTooltip() {
        return this.result && this.attachedResults?.isAttached(this.result) ? this.bindings.i18n.t('detach-from-case') : this.bindings.i18n.t('attach-to-case');
    }
    render() {
        if (!this.result || !this.attachedResults) return html``;
        return renderIconButton({
            props: {
                partPrefix: 'result-action',
                style: 'outline-neutral',
                icon: this.getIcon(),
                ariaLabel: this.getTooltip(),
                title: this.getTooltip(),
                onClick: ()=>this.onClick()
            }
        });
    }
    constructor(){
        super();
        this.itemContextController = new ItemContextController(this, {
            parentName: 'atomic-insight-result',
            folded: false
        });
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightResultAttachToCaseAction.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightResultAttachToCaseAction.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('attachedResults'),
    state(),
    _ts_metadata("design:type", "u" < typeof Record ? Object : Record)
], AtomicInsightResultAttachToCaseAction.prototype, "attachedResultsState", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightResultAttachToCaseAction.prototype, "render", null);
AtomicInsightResultAttachToCaseAction = _ts_decorate([
    customElement('atomic-insight-result-attach-to-case-action'),
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicInsightResultAttachToCaseAction);
export { AtomicInsightResultAttachToCaseAction };
