import type { Layer } from '../providers/layers/types'

/**
 * Maps a layer's legend configuration to the shape expected by the Meridian Design System `LegendWidgetUI` component.
 *
 * @param layer - The layer whose legend properties to transform.
 * @returns An object with properties like `title`, `legend`, `opacity`, `visible`, `collapsed`, and zoom-level bounds.
 *
 * @example
 * ```tsx
 * const { legend } = useLegend([LAYER_ID], mappingLegendMeridian)
 * ```
 *
 * @deprecated Used together with the deprecated `useLegend`. The react-ui
 * legend module (`@carto/ps-react-ui/legend`) now owns legend shaping via its
 * own discriminated-union `variables` contract. Will be removed alongside
 * `useLegend`.
 */
export function mappingLegendMeridian<T>(layer: Layer) {
  return {
    collapsed: layer.legend?.collapsed,
    collapsible: layer.legend?.collapsible,
    helperText: layer.legend?.helperText,
    id: layer.props.id,
    legend: layer.legend?.variables,
    maxZoom: layer.legend?.maxZoom,
    minZoom: layer.legend?.minZoom,
    opacity: layer.props.opacity ?? 1,
    showOpacityControl: layer.legend?.opacityControl,
    switchable: layer.legend?.switchable,
    title: layer.name,
    visible: layer.props.visible ?? true,
  } as T
}
