import { css, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators";
import { until } from "lit/directives/until";
import {
DEFAULT_DOMAIN_ICON,
domainIcon,
FALLBACK_DOMAIN_ICONS,
} from "../data/icons";
import type { HomeAssistant } from "../types";
import { brandsUrl } from "../util/brands-url";
import "./ha-icon";
@customElement("ha-domain-icon")
export class HaDomainIcon extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@property() public domain?: string;
@property({ attribute: false }) public deviceClass?: string;
@property({ attribute: false }) public state?: string;
@property() public icon?: string;
@property({ attribute: "brand-fallback", type: Boolean })
public brandFallback?: boolean;
protected render() {
if (this.icon) {
return html``;
}
if (!this.domain) {
return nothing;
}
if (!this.hass) {
return this._renderFallback();
}
const icon = domainIcon(
this.hass,
this.domain,
this.deviceClass,
this.state
).then((icn) => {
if (icn) {
return html``;
}
return this._renderFallback();
});
return html`${until(icon)}`;
}
private _renderFallback() {
if (this.domain && this.domain in FALLBACK_DOMAIN_ICONS) {
return html`
`;
}
if (this.brandFallback) {
const image = brandsUrl({
domain: this.domain!,
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
});
return html`
`;
}
return html``;
}
static styles = css`
img {
width: var(--mdc-icon-size, 24px);
}
`;
}
declare global {
interface HTMLElementTagNameMap {
"ha-domain-icon": HaDomainIcon;
}
}