import { useMemo } from "react";
import { isNonNullable } from "../utils/isNonNullable";
import { useFiles, useSwatches } from "../../runtimes/react/hooks/makeswift-api";
import { match, P } from "ts-pattern";
import {
  getBackgroundsPropControllerDataResponsiveBackgroundsData,
  getBackgroundsPropControllerFileIds,
  getBackgroundsPropControllerSwatchIds
} from "@makeswift/prop-controllers";
function useBackgrounds(data) {
  const value = getBackgroundsPropControllerDataResponsiveBackgroundsData(data);
  const fileIds = getBackgroundsPropControllerFileIds(data);
  const files = useFiles(fileIds);
  const swatchIds = getBackgroundsPropControllerSwatchIds(data);
  const swatches = useSwatches(swatchIds);
  return useMemo(() => {
    if (value == null)
      return void 0;
    return value.map(({ value: backgrounds, ...restOfValue }) => ({
      ...restOfValue,
      value: backgrounds.map((bg) => {
        if (bg.type === "image" && bg.payload != null) {
          const { imageId, ...restOfPayload } = bg.payload;
          const file = files.find((f) => f && f.id === imageId);
          return file && {
            ...bg,
            payload: {
              ...restOfPayload,
              publicUrl: file.publicUrl,
              dimensions: file.dimensions
            }
          };
        }
        if (bg.type === "image-v1" && bg.payload != null) {
          return match(bg).with(
            {
              payload: { image: { type: "external-file", width: P.number, height: P.number } }
            },
            (val) => {
              const { image, version, ...restOfPayload } = val.payload;
              return {
                ...val,
                type: "image",
                payload: {
                  ...restOfPayload,
                  publicUrl: image.url,
                  dimensions: { width: image.width, height: image.height }
                }
              };
            }
          ).with({ payload: { image: { type: "external-file" } } }, (val) => {
            const { image, version, ...restOfPayload } = val.payload;
            return {
              ...val,
              type: "image",
              payload: {
                ...restOfPayload,
                publicUrl: image.url,
                dimensions: null
              }
            };
          }).with({ payload: { image: { type: "makeswift-file" } } }, (val) => {
            const { image, version, ...restOfPayload } = val.payload;
            const file = files.find((f) => f && f.id === image.id);
            return file && {
              ...val,
              type: "image",
              payload: {
                ...restOfPayload,
                publicUrl: file.publicUrl,
                dimensions: file.dimensions
              }
            };
          }).otherwise(() => null);
        }
        if (bg.type === "color" && bg.payload != null) {
          const { swatchId, alpha } = bg.payload;
          const swatch = swatches.filter(isNonNullable).find((s) => s && s.id === swatchId);
          return { id: bg.id, type: "color", payload: { swatch, alpha } };
        }
        if (bg.type === "gradient" && bg.payload != null && bg.payload.stops.length > 0) {
          return {
            id: bg.id,
            type: "gradient",
            payload: {
              angle: bg.payload.angle,
              isRadial: bg.payload.isRadial,
              stops: bg.payload.stops.map(({ color, ...restOfStop }) => ({
                ...restOfStop,
                color: color && {
                  swatch: swatches.filter(isNonNullable).find((s) => s && s.id === color.swatchId),
                  alpha: color.alpha
                }
              }))
            }
          };
        }
        if (bg.type === "video" && bg.payload != null) {
          const { maskColor, ...restOfPayload } = bg.payload;
          const swatch = maskColor && swatches.find((s) => s && s.id === maskColor.swatchId);
          return {
            id: bg.id,
            type: "video",
            payload: {
              ...restOfPayload,
              maskColor: swatch && maskColor && { swatch, alpha: maskColor.alpha }
            }
          };
        }
        return null;
      }).filter(isNonNullable)
    }));
  }, [files, swatches, value]);
}
export {
  useBackgrounds
};
//# sourceMappingURL=useBackgrounds.js.map