import classNames from 'classnames'
import { useDeferredValue, useMemo, useState } from 'react'
import type {
CompatiblePartner,
PartnerAsset,
PartnerAssetKind,
} from 'lib/seam/ecosystem/types.js'
import { useCompatiblePartners } from 'lib/seam/ecosystem/use-compatible-partners.js'
import { getEcosystemErrorMessage } from 'lib/ui/ecosystem/errors.js'
import { SearchField } from 'lib/ui/ecosystem/SearchField.js'
import { StateMessage } from 'lib/ui/ecosystem/StateMessage.js'
import {
type EcosystemTheme,
getEcosystemThemeClassName,
} from 'lib/ui/ecosystem/theme.js'
export interface CompatibleAppsProps {
/** Device category, e.g., `smartlock`, `thermostat`, `sensor`, `intercom`. */
deviceType: string
/** DeviceDB manufacturer slug. Lists the apps that support this brand. */
manufacturer?: string | undefined
/** DeviceDB provider slug, i.e., the cloud the devices connect through. */
provider?: string | undefined
/** DeviceDB capability, e.g., `can_provision_mobile_keys`. Shows per-app notes. */
capability?: string | undefined
/** Industry slug to restrict the apps, e.g., `short-term-rental`. */
industry?: string | undefined
/** Apps shown before the show more button. */
maxVisible?: number | undefined
hideSearch?: boolean | undefined
hideTitle?: boolean | undefined
/** Color theme. `auto` follows the visitor's color scheme. */
theme?: EcosystemTheme | undefined
className?: string | undefined
}
const defaultMaxVisible = 24
/**
* The live apps (Seam partners) that work with a device category,
* manufacturer, provider or capability through Seam.
*/
export function CompatibleApps({
deviceType,
manufacturer = '',
provider = '',
capability = '',
industry = '',
maxVisible = defaultMaxVisible,
hideSearch = false,
hideTitle = false,
theme = 'light',
className,
}: CompatibleAppsProps): JSX.Element {
const { partners, isPending, isError, error, refetch } =
useCompatiblePartners({
deviceType: deviceType ?? '',
manufacturer,
provider,
capability,
industry,
})
const [searchValue, setSearchValue] = useState('')
const [showAll, setShowAll] = useState(false)
const query = useDeferredValue(searchValue.toLowerCase().trim())
const sortedPartners = useMemo(
() => [...(partners ?? [])].sort(comparePartners),
[partners]
)
const filteredPartners = useMemo(
() =>
query.length === 0
? sortedPartners
: sortedPartners.filter((partner) => matchesSearch(partner, query)),
[sortedPartners, query]
)
const visibleCount = getVisibleCount(maxVisible)
const visiblePartners = showAll
? filteredPartners
: filteredPartners.slice(0, visibleCount)
const hiddenCount = filteredPartners.length - visiblePartners.length
const showNotes = capability.length > 0 || provider.length > 0
const isMissingDeviceType = (deviceType ?? '').trim().length === 0
const content = (() => {
if (isMissingDeviceType) {
return