import { mdiClockOutline, mdiShape, mdiWeatherSunny } from "@mdi/js";
import type { Connection } from "home-assistant-js-websocket";
import { computeDomain } from "../common/entity/compute_domain";
import { computeObjectId } from "../common/entity/compute_object_id";
import type { AutomationElementGroupCollection } from "./automation";
import type { Selector, TargetSelector } from "./selector";

export const CONDITION_COLLECTIONS: AutomationElementGroupCollection[] = [
  {
    groups: {
      dynamicGroups: {},
      time: {
        icon: mdiClockOutline,
        members: { time: {} },
        domains: ["calendar", "schedule"],
      },
      sun: {
        icon: mdiWeatherSunny,
        domains: ["sun"],
      },
      helpers: {},
      template: {},
      trigger: {},
    },
  },
  {
    titleKey:
      "ui.panel.config.automation.editor.conditions.groups.generic.label",
    generic: true,
    groups: {
      device: {},
      entity: { icon: mdiShape, members: { state: {}, numeric_state: {} } },
    },
  },
  {
    titleKey:
      "ui.panel.config.automation.editor.conditions.groups.integrations.label",
    groups: {
      integrationGroups: {},
    },
  },
] as const;

export const CONDITION_BUILDING_BLOCKS_GROUP = {
  and: {},
  or: {},
  not: {},
};

export const CONDITION_BUILDING_BLOCKS = ["and", "or", "not"];

export const COLLAPSIBLE_CONDITION_ELEMENTS = [
  "ha-automation-condition-and",
  "ha-automation-condition-not",
  "ha-automation-condition-or",
];

export interface ConditionDescription {
  target?: TargetSelector["target"];
  fields: Record<
    string,
    {
      example?: string | boolean | number;
      default?: unknown;
      required?: boolean;
      selector?: Selector;
      context?: Record<string, string>;
    }
  >;
}

export type ConditionDescriptions = Record<string, ConditionDescription>;

export const subscribeConditions = (
  connection: Connection,
  callback: (conditions: ConditionDescriptions) => void
) =>
  connection.subscribeMessage<ConditionDescriptions>(callback, {
    type: "condition_platforms/subscribe",
  });

export const getConditionDomain = (condition: string) =>
  condition.includes(".") ? computeDomain(condition) : condition;

export const getConditionObjectId = (condition: string) =>
  condition.includes(".") ? computeObjectId(condition) : "_";
