'use strict';
import type { ViewStyle } from 'react-native';

import { isNumber, maybeAddSuffix } from '../../../utils';
import type { ValueProcessor } from '../types';
import { processColor } from './colors';

type BackgroundImageStyleValue = NonNullable<
  ViewStyle[Extract<
    keyof ViewStyle,
    'backgroundImage' | 'experimental_backgroundImage'
  >]
>;
type BackgroundImageValue = Exclude<BackgroundImageStyleValue, string>[number];
type RadialGradientValue = Extract<
  BackgroundImageValue,
  { type: 'radial-gradient' }
>;

const processColorStops = (colorStops: BackgroundImageValue['colorStops']) =>
  colorStops
    .map(({ color, positions }) => {
      const positionsString =
        positions
          ?.map((position) => maybeAddSuffix(position, 'px'))
          .join(' ') ?? '';
      if (color == null) {
        return positionsString;
      }
      const processedColor = processColor(color) as string | undefined;
      return [processedColor, positionsString].filter(Boolean).join(' ');
    })
    .join(', ');

const processRadialShapeAndSize = (
  shape: RadialGradientValue['shape'],
  size: RadialGradientValue['size']
) => {
  if (typeof size === 'string') {
    return `${shape} ${size}`;
  }
  if (shape === 'circle' && isNumber(size.x) && isNumber(size.y)) {
    return `circle ${Math.max(size.x, size.y)}px`;
  }
  return `ellipse ${maybeAddSuffix(size.x, 'px')} ${maybeAddSuffix(size.y, 'px')}`;
};

const processRadialPosition = (position: RadialGradientValue['position']) =>
  Object.entries(position)
    .map(([edge, offset]) => `${edge} ${maybeAddSuffix(offset, 'px')}`)
    .join(' ');

export const processBackgroundImageWeb: ValueProcessor<
  BackgroundImageStyleValue
> = (value) => {
  if (typeof value === 'string') {
    return value;
  }

  return value
    .map((backgroundImage) => {
      const colorStops = processColorStops(backgroundImage.colorStops);
      if (backgroundImage.type === 'linear-gradient') {
        return `linear-gradient(${backgroundImage.direction ?? 'to bottom'}, ${colorStops})`;
      }
      const {
        shape = 'ellipse',
        size = 'farthest-corner',
        position = { top: '50%', left: '50%' },
      } = backgroundImage;
      return `radial-gradient(${processRadialShapeAndSize(shape, size)} at ${processRadialPosition(position)}, ${colorStops})`;
    })
    .join(', ');
};
