import lodash from "lodash";
import { firstRight } from "../../fp-utils.js";
const imageElementRegex = /^wix:image:\/\/v1\/(.+)\/(.+)#originWidth=(\d+)&originHeight=(\d+)/i;
const urlRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/g;
const parseImageElement = (data) => {
  const vars = imageElementRegex.exec(data.src);
  if (typeof data.src !== "string" || vars === null) {
    throw new Error("Source format is invalid. Received:\n" + JSON.stringify(data));
  }
  const { alt } = data;
  const [, uri, fileName, originWidth, originHeight] = vars;
  return { uri, fileName, width: parseInt(originWidth), height: parseInt(originHeight), alt };
};
function isImageElement(data) {
  return !!data && !lodash.isString(data) && !!Object.keys(data).includes("src");
}
const asPublicUrl = (data) => ({
  image: { src: { url: data } }
});
const asImageElement = (data) => {
  if (data.src.match(urlRegex)) {
    return { image: { src: { url: data.src } } };
  }
  const { width, height, uri: id, alt } = parseImageElement(data);
  return { altText: alt, image: { src: { id }, width, height } };
};
const toImageData = (data) => firstRight(data, data, [
  // @ts-ignore $TsFixMe
  [lodash.isString, asPublicUrl],
  // @ts-ignore $TsFixMe
  [isImageElement, asImageElement]
]);
const addImage = ({
  data,
  ...rest
} = {}) => ({
  data: toImageData(data),
  ...rest
});
const createImageData = (data) => toImageData(data);
export {
  addImage,
  createImageData
};
//# sourceMappingURL=add-image.js.map
