import { loadInsightAnalyticsActions } from "@coveo/headless/insight";
import { LitElement, css } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { renderIconButton } from "../../common/icon-button.js";
import { ItemContextController } from "../../common/item-list/context/item-context-controller.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 copy_dark from "../../../images/copy-dark.js";
import email from "../../../images/email.js";
import preview from "../../../images/preview.js";
import share_post from "../../../images/share-post.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);
}
var atomic_insight_result_action_Actions = /*#__PURE__*/ function(Actions) {
    Actions["CopyToClipboard"] = "copyToClipboard";
    Actions["AttachToCase"] = "attachToCase";
    Actions["Quickview"] = "quickview";
    Actions["PostToFeed"] = "postToFeed";
    Actions["SendAsEmail"] = "sendAsEmail";
    return Actions;
}({});
class AtomicInsightResultAction extends LightDomMixin(LitElement) {
    disconnectedCallback() {
        super.disconnectedCallback();
        clearTimeout(this.tooltipResetTimeout);
    }
    get result() {
        return this.itemContextController.item;
    }
    initialize() {
        this.actions = loadInsightAnalyticsActions(this.bindings.engine);
        this.currentTooltip = this.tooltip;
    }
    onClick() {
        if (!this.result) return;
        if (this.tooltipOnClick) {
            const originalTooltip = this.tooltip;
            this.currentTooltip = this.tooltipOnClick;
            clearTimeout(this.tooltipResetTimeout);
            this.tooltipResetTimeout = setTimeout(()=>{
                this.currentTooltip = originalTooltip;
            }, 1000);
        }
        switch(this.action){
            case "copyToClipboard":
                this.bindings.engine.dispatch(this.actions.logCopyToClipboard(this.result));
                navigator.clipboard.writeText(this.result.clickUri);
                break;
            case "postToFeed":
                this.bindings.engine.dispatch(this.actions.logFeedItemTextPost(this.result));
                break;
            case "sendAsEmail":
                this.bindings.engine.dispatch(this.actions.logCaseSendEmail(this.result));
                break;
        }
        this.dispatchEvent(new CustomEvent('atomicInsightResultActionClicked', {
            bubbles: true,
            composed: true,
            cancelable: true,
            detail: {
                action: this.action,
                result: this.result
            }
        }));
    }
    getIcon() {
        if (this.icon) return this.icon;
        switch(this.action){
            case "copyToClipboard":
                return copy_dark;
            case "attachToCase":
                return attach;
            case "quickview":
                return preview;
            case "postToFeed":
                return share_post;
            case "sendAsEmail":
                return email;
            default:
                return preview;
        }
    }
    render() {
        if (!this.result) return null;
        return renderIconButton({
            props: {
                partPrefix: 'result-action',
                style: 'outline-neutral',
                icon: this.getIcon(),
                title: this.currentTooltip,
                onClick: ()=>this.onClick()
            }
        });
    }
    constructor(){
        super(), this.icon = '', this.tooltip = '', this.tooltipOnClick = '', this.action = '', this.currentTooltip = '';
        this.itemContextController = new ItemContextController(this, {
            parentName: 'atomic-insight-result',
            folded: false
        });
    }
}
AtomicInsightResultAction.styles = css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */[part=result-action-button]{align-items:center;display:flex;height:calc(var(--spacing,.25rem)*8);justify-content:center;width:calc(var(--spacing,.25rem)*8)}`;
_ts_decorate([
    property({
        type: String
    })
], AtomicInsightResultAction.prototype, "icon", void 0);
_ts_decorate([
    property({
        type: String
    })
], AtomicInsightResultAction.prototype, "tooltip", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'tooltip-on-click'
    })
], AtomicInsightResultAction.prototype, "tooltipOnClick", void 0);
_ts_decorate([
    property({
        type: String
    }),
    _ts_metadata("design:type", Object)
], AtomicInsightResultAction.prototype, "action", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightResultAction.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightResultAction.prototype, "error", void 0);
_ts_decorate([
    state()
], AtomicInsightResultAction.prototype, "currentTooltip", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightResultAction.prototype, "render", null);
AtomicInsightResultAction = _ts_decorate([
    customElement('atomic-insight-result-action'),
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicInsightResultAction);
export { AtomicInsightResultAction, atomic_insight_result_action_Actions as Actions };
