"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useBackgrounds_exports = {};
__export(useBackgrounds_exports, {
  useBackgrounds: () => useBackgrounds
});
module.exports = __toCommonJS(useBackgrounds_exports);
var import_react = require("react");
var import_isNonNullable = require("../utils/isNonNullable");
var import_makeswift_api = require("../../runtimes/react/hooks/makeswift-api");
var import_ts_pattern = require("ts-pattern");
var import_prop_controllers = require("@makeswift/prop-controllers");
function useBackgrounds(data) {
  const value = (0, import_prop_controllers.getBackgroundsPropControllerDataResponsiveBackgroundsData)(data);
  const fileIds = (0, import_prop_controllers.getBackgroundsPropControllerFileIds)(data);
  const files = (0, import_makeswift_api.useFiles)(fileIds);
  const swatchIds = (0, import_prop_controllers.getBackgroundsPropControllerSwatchIds)(data);
  const swatches = (0, import_makeswift_api.useSwatches)(swatchIds);
  return (0, import_react.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 (0, import_ts_pattern.match)(bg).with(
            {
              payload: { image: { type: "external-file", width: import_ts_pattern.P.number, height: import_ts_pattern.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(import_isNonNullable.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(import_isNonNullable.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(import_isNonNullable.isNonNullable)
    }));
  }, [files, swatches, value]);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useBackgrounds
});
//# sourceMappingURL=useBackgrounds.js.map