import { ResultTemplatesHelpers } from "@coveo/headless";
import bg_standard_common from "@salesforce-ux/design-system/design-tokens/dist/bg-standard.common";
import { LitElement, css, html, nothing } from "lit";
import { customElement, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import "../../common/atomic-icon/atomic-icon.js";
import { createResultContextController } from "../result-template-component-utils/context/result-context-controller.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { snakeToCamel } from "../../../utils/utils.js";
import { fileTypeIcons } from "./file-type-icons.js";
import { objectTypeIcons } from "./object-type-icons.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 AtomicResultIcon extends LitElement {
    initialize() {}
    get result() {
        const item = this.resultController.item;
        if (!item) return null;
        if ('result' in item) return item.result;
        return item;
    }
    get icon() {
        if (!this.result) return null;
        const fileTypeValue = ResultTemplatesHelpers.getResultProperty(this.result, 'filetype');
        const objectTypeValue = ResultTemplatesHelpers.getResultProperty(this.result, 'objecttype');
        const fileType = fileTypeIcons[fileTypeValue?.toLowerCase()];
        const objectType = objectTypeIcons[objectTypeValue?.toLowerCase()];
        return objectType ?? fileType ?? null;
    }
    renderIcon() {
        const icon = this.icon || 'document';
        const backgroundColor = bg_standard_common[snakeToCamel(icon)] || 'transparent';
        return html`
      <atomic-icon
        icon=${`assets://${icon}`}
        class=${icon}
        title=${icon}
        style="background-color: ${backgroundColor}"
      ></atomic-icon>
    `;
    }
    render() {
        if (this.resultController.hasError) return nothing;
        return when(this.icon, ()=>this.renderIcon(), ()=>html`<slot>${this.renderIcon()}</slot>`);
    }
    constructor(...args){
        super(...args), this.license = "Salesforce's specific icons originally from lightning design system (https://www.lightningdesignsystem.com/icons/). Icons licensed under Creative Commons Attribution-NoDerivatives 4.0 (https://github.com/salesforce-ux/design-system).", this.resultController = createResultContextController(this);
    }
}
AtomicResultIcon.styles = css`:host{display:inline-block;height:auto;width:100%}atomic-icon{width:100%}`;
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultIcon.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultIcon.prototype, "error", void 0);
_ts_decorate([
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultIcon.prototype, "render", null);
AtomicResultIcon = _ts_decorate([
    customElement('atomic-result-icon'),
    bindings()
], AtomicResultIcon);
export { AtomicResultIcon };
