import { customElement } from "lit/decorators";
import { assert, assign, boolean, object, optional, string } from "superstruct";
import type {
  EntityBadgeConfig,
  StateLabelBadgeConfig,
} from "../../badges/types";
import { actionConfigStruct } from "../structs/action-struct";
import { baseLovelaceBadgeConfig } from "../structs/base-badge-struct";
import { HuiEntityBadgeEditor } from "./hui-entity-badge-editor";

const badgeConfigStruct = assign(
  baseLovelaceBadgeConfig,
  object({
    entity: optional(string()),
    name: optional(string()),
    icon: optional(string()),
    color: optional(string()),
    show_entity_picture: optional(boolean()),
    tap_action: optional(actionConfigStruct),
    show_name: optional(boolean()),
    image: optional(string()),
  })
);

@customElement("hui-state-label-badge-editor")
export class HuiStateLabelBadgeEditor extends HuiEntityBadgeEditor {
  // @ts-ignore
  public override setConfig(config: StateLabelBadgeConfig): void {
    assert(config, badgeConfigStruct);

    const entityBadgeConfig: EntityBadgeConfig = {
      type: "entity",
      entity: config.entity,
      color: config.color,
      show_name: config.show_name ?? true,
    };

    super.setConfig(entityBadgeConfig);
  }
}

declare global {
  interface HTMLElementTagNameMap {
    "hui-state-label-badge-editor": HuiStateLabelBadgeEditor;
  }
}
