import memoizeOne from "memoize-one";
import type { DeviceRegistryEntry } from "../../data/device/device_registry";
import type {
  EntityRegistryDisplayEntry,
  EntityRegistryEntry,
} from "../../data/entity/entity_registry";
import type { HomeAssistant } from "../../types";
import { getDuplicates } from "../string/get_duplicates";
import { computeStateName } from "./compute_state_name";

export const computeDeviceName = (
  device: DeviceRegistryEntry
): string | undefined => (device.name_by_user || device.name)?.trim();

export const computeDeviceNameDisplay = (
  device: DeviceRegistryEntry,
  localize: HomeAssistant["localize"],
  hassStates: HomeAssistant["states"],
  entities?: EntityRegistryEntry[] | EntityRegistryDisplayEntry[] | string[]
) =>
  computeDeviceName(device) ||
  (entities && fallbackDeviceName(hassStates, entities)) ||
  localize("ui.panel.config.devices.unnamed_device", {
    type: localize(
      `ui.panel.config.devices.type.${device.entry_type || "device"}`
    ),
  });

export const fallbackDeviceName = (
  hassStates: HomeAssistant["states"],
  entities: EntityRegistryEntry[] | EntityRegistryDisplayEntry[] | string[]
) => {
  for (const entity of entities || []) {
    const entityId = typeof entity === "string" ? entity : entity.entity_id;
    const stateObj = hassStates[entityId];
    if (stateObj) {
      return computeStateName(stateObj);
    }
  }
  return undefined;
};

export const getDuplicatedDeviceNames = memoizeOne(
  (devices: HomeAssistant["devices"]): Set<string> => {
    const names = Object.values(devices)
      .map((device) => computeDeviceName(device))
      .filter((name): name is string => name !== undefined);

    return getDuplicates(names);
  }
);
