import { jsxs as v, jsx as l, Fragment as Ie } from "react/jsx-runtime";
/* empty css                   */
import { useToggle as ye } from "./index-useToggle.js";
import { mb1 as he, gap1 as Me, dFlexColumn as Te, dFlex as Oe } from "./index-general.module.js";
import { classNames as D } from "./index-classNames.js";
import { downloadZipFile as Ee } from "./index-download-helpers.js";
import { uuidv4 as _e } from "./index-generate-uid.js";
import { LocationMediaItemScope as Pe } from "./index-LocationMediaItem.js";
import { ContentType as d } from "./index-Document.types.js";
import { ImageContentProcessor as be } from "./index-ImageContentProcessor.js";
import { ImageMetadataProcessor as ve } from "./index-ImageMetadataProcessor.js";
import { VideoMetadataProcessor as ge } from "./index-VideoMetadataProcessor.js";
import { VideoThumbnailProcessor as xe } from "./index-VideoThumbnailProcessor.js";
import { ImageCompressorTransformer as Ae } from "./index-ImageCompressorTransformer.js";
import { ImageCropperTransformer as Se } from "./index-ImageCropperTransformer.js";
import { FileSizeValidator as k } from "./index-FileSizeValidator.js";
import { FileTypeValidator as De } from "./index-FileTypeValidator.js";
import { ImageResolutionValidator as Ne } from "./index-ImageResolutionValidator.js";
import { VideoDurationValidator as we } from "./index-VideoDurationValidator.js";
import { VideoResolutionValidator as Ve } from "./index-VideoResolutionValidator.js";
import { POST_VIDEO_VALIDATION_REQUIREMENTS as c, IMAGE_VALIDATION_ERRORS as Ce } from "./index-media-items-validations.js";
import { Icon as Fe } from "./index-Icon.js";
import { InfoBox as Re } from "./index-InfoBox.js";
import { Popover as Ge } from "./index-Popover.js";
import { Render as ke } from "./index-Render.js";
import { Text as N, TextTypes as w, FontWeights as He } from "./index-Text.js";
import { useState as V, useEffect as We } from "react";
import { MediaItemSelectInput as Le } from "./index-MediaItemSelectInput.js";
import { IconNames as I } from "./index-IconNames.js";
import { ModalService as H } from "./index-ModalService.js";
import { snackbarService as W } from "./index-SnackBarService.js";
import { GalleryFeedbackModal as ze } from "./index-GalleryFeedbackModal.js";
import { GalleryMediaItem as Be } from "./index-GalleryMediaItem.js";
import { PhotoPlaceholder as Ue } from "./index-PhotoPlaceholder.js";
const vt = Pe.PHOTO, M = (T) => T.map((u) => ({
  ...u,
  uuid: u.uuid ?? _e()
})), gt = (T) => {
  var R, G;
  const {
    mediaItems: u = [],
    editMode: O,
    placeholderConfig: p,
    onChange: E,
    onSecondaryChange: C,
    canDownload: L,
    downloading: g,
    downloadCallback: z,
    className: B = ""
  } = T;
  let {
    config: e
  } = T;
  e || (e = {}), (e == null ? void 0 : e.showFeedbackModal) === void 0 && (e.showFeedbackModal = !0);
  const _ = (e == null ? void 0 : e.maxNumberOfMediaItems) ?? 30, x = (e == null ? void 0 : e.acceptTypes) ?? [d.IMAGE_JPEG, d.IMAGE_PNG, d.IMAGE_JPG], U = x.some((t) => t.includes("video")) ? [I.image, I.media] : [I.image], A = (e == null ? void 0 : e.alertType) ?? "snackbar", X = (e == null ? void 0 : e.forceOpenFileSelect) ?? !1, y = {
    maxWidth: 3e3,
    maxHeight: 3e3,
    minHeight: 720,
    minWidth: 720,
    quality: 0.9,
    convertSize: 5242880,
    ...e.compressorOptions
  }, J = e.useCropper ? new Se({
    options: e.cropperOptions
  }) : null, Y = e.useCompressor ? new Ae({
    compressorOptions: y,
    showLoadingModalDuringCompression: e.showLoadingModalDuringCompression
  }) : null, j = new be({}), Z = new ve({}), $ = new xe({}), q = new ge({}), Q = new De({
    applicableFileTypes: [d.FILE_ANY],
    acceptedTypes: x
  }), K = new k({
    applicableFileTypes: [d.IMAGE_ANY, d.IMAGE_JPEG, d.IMAGE_JPG, d.IMAGE_PNG],
    maxSize: 5242880,
    minSize: 10240
    // 10 KB in bytes
  }), ee = new k({
    applicableFileTypes: [d.VIDEO_ANY, d.VIDEO_MOV, d.VIDEO_MP4],
    maxSize: c.POST_VIDEO_MAX_SIZE,
    minSize: c.POST_VIDEO_MIN_SIZE
  }), te = new we({
    minDuration: c.POST_VIDEO_MIN_DURATION,
    maxDuration: c.POST_VIDEO_MAX_DURATION
  }), ie = new Ne({
    minWidth: y.minWidth,
    minHeight: y.minHeight,
    maxWidth: y.maxWidth,
    maxHeight: y.maxHeight,
    minAspectRatio: (R = e == null ? void 0 : e.cropperOptions) == null ? void 0 : R.aspectRatio,
    maxAspectRatio: (G = e == null ? void 0 : e.cropperOptions) == null ? void 0 : G.secondaryAspectRatio
  }), oe = new Ve({
    minWidth: c.POST_VIDEO_MIN_WIDTH,
    minHeight: c.POST_VIDEO_MIN_HEIGHT,
    maxWidth: c.POST_VIDEO_MAX_WIDTH,
    maxHeight: c.POST_VIDEO_MAX_HEIGHT,
    minAspectRatio: c.POST_VIDEO_MIN_ASPECT_RATIO,
    maxAspectRatio: c.POST_VIDEO_MAX_ASPECT_RATIO
  }), [re, F] = ye(X), [f, S] = V(M(u)), [ae, se] = V(M([])), [h, P] = V({
    title: "",
    description: "",
    dismissible: !0,
    position: "start"
  });
  We(() => {
    S(M(u));
  }, [u]);
  const ne = (t) => {
    const i = f.filter((o) => o.uuid !== t.uuid);
    S(i), E && E("DELETE", [t], i);
  }, me = (t) => {
    const i = f.filter((o) => o.uuid !== t.uuid);
    Ee([{
      src: i[0].publicUrl ?? "",
      name: i[0].mediaItemName ?? "img"
    }], "zipfile");
  }, le = (t) => {
    const i = Array.isArray(t) ? t : [t];
    if ([...f, ...i].length > _) {
      b("Please select fewer media items");
      return;
    }
    if (!(e != null && e.acceptDifferentFileTypes) && [...new Set(i.map((n) => {
      var s, m;
      return (m = (s = n.file) == null ? void 0 : s.type) == null ? void 0 : m.split("/")[0];
    }))].length > 1 && e && e.maxNumberOfMediaItemsPerType && e.maxNumberOfMediaItemsPerType.video && e.maxNumberOfMediaItemsPerType.image) {
      P({
        ...h,
        title: "Reduce media",
        replacements: {
          videoCount: e.maxNumberOfMediaItemsPerType.video,
          imagesCount: e.maxNumberOfMediaItemsPerType.image
        },
        description: "Only one type of media is allowed — up to %imagesCount% photos or %videoCount% video.",
        type: "danger"
      });
      return;
    }
    if (e != null && e.maxNumberOfMediaItemsPerType) {
      const r = i.filter((s) => {
        var m;
        return (m = s.fileType) == null ? void 0 : m.includes("video");
      }), n = i.filter((s) => {
        var m;
        return (m = s.fileType) == null ? void 0 : m.includes("image");
      });
      if (e.maxNumberOfMediaItemsPerType.video && r.length > e.maxNumberOfMediaItemsPerType.video) {
        b("Each post can include only %videoCount% video.", {
          videoCount: e.maxNumberOfMediaItemsPerType.video
        });
        return;
      }
      if (e.maxNumberOfMediaItemsPerType.image && n.length > e.maxNumberOfMediaItemsPerType.image) {
        b("Each post can include only %imagesCount% images.", {
          imagesCount: e.maxNumberOfMediaItemsPerType.image
        });
        return;
      }
    }
    e != null && e.removeDuplicates && i.map((r) => {
      f.find((n) => {
        var s, m;
        return ((s = n == null ? void 0 : n.file) == null ? void 0 : s.name) === ((m = r.file) == null ? void 0 : m.name);
      }) && (r.isInvalid = !0, r.validationErrors || (r.validationErrors = []), r.validationErrors.push({
        text: Ce.DUPLICATE
      }));
    });
    const a = Array.isArray(t) ? t : [t];
    e != null && e.showFeedbackModal && fe(a), de(a);
  }, de = (t) => {
    const i = t.filter((s) => !s.isInvalid);
    i === void 0 || i.length;
    const o = i.filter((s) => s.filePriority === "primary"), a = i.filter((s) => s.filePriority === "secondary"), r = [...f, ...o], n = [...ae, ...a];
    S(M(r)), se(M(n)), E && E("ADD", o, r), C && C("ADD", a, n);
  }, ce = (t) => {
    var a;
    if (((a = t == null ? void 0 : t.settings) == null ? void 0 : a.toBeDeleted) ?? !1)
      return [];
    const o = [{
      text: "Preview",
      icon: I.visibility,
      callback: (r) => {
        const n = f.filter((s) => {
          var m;
          return !(((m = s == null ? void 0 : s.settings) == null ? void 0 : m.toBeDeleted) ?? !1);
        });
        H.openPhotoGalleryModal(n, r);
      }
    }];
    return O && o.push({
      text: "Delete",
      icon: I.trash,
      callback: ne
    }), L && o.push({
      text: "Download",
      disabled: g,
      icon: l(Fe, {
        className: `${g ? "loader-spinner" : ""}`,
        color: "--n100",
        children: g ? I.save : I.download
      }),
      callback: z ?? me
    }), o;
  }, b = (t, i) => {
    switch (A) {
      case "inline":
        P({
          ...h,
          title: t,
          description: "",
          replacements: i,
          type: "danger"
        });
        break;
      case "snackbar":
        W.openErrorSnackbar({
          text: t,
          replacements: i
        });
        break;
    }
  }, pe = (t) => {
    var o, a;
    if (t.length !== 1)
      return;
    const i = t[0];
    if (!i.isInvalid) {
      A === "snackbar" && W.openSuccessSnackbar("The media item was successfully added to the gallery.");
      return;
    }
    if ((o = i.validationErrors) != null && o.length) {
      const {
        text: r,
        replacements: n
      } = (a = i.validationErrors) == null ? void 0 : a[0];
      b(r, n);
    }
  }, ue = (t) => {
    var o;
    const i = {};
    if (!t)
      return i;
    for (const a of t)
      if (a.isInvalid)
        for (const r of a.validationErrors ?? []) {
          const n = ((o = a.file) == null ? void 0 : o.name) ?? "";
          if (i[r.text]) {
            if (i[r.text].mediaItemsWithErrors.includes(n))
              continue;
            i[r.text].mediaItemsWithErrors.push(n);
          } else
            i[r.text] = {
              mediaItemsWithErrors: [n],
              replacements: r.replacements
            };
        }
    return i;
  }, fe = (t) => {
    if (t.length === 1) {
      pe(t);
      return;
    }
    const i = ue(t);
    if (A === "inline") {
      const o = t.filter((a) => a.isInvalid).length;
      o && P({
        ...h,
        title: "We have identified %no_of_issues% issue(s) with the media items you've uploaded.",
        replacements: {
          no_of_issues: o
        },
        description: l(Ie, {
          children: Object.keys(i).map((a, r) => v("div", {
            className: D(he),
            children: [l(N, {
              type: w.textHelp,
              replacements: i[a].replacements,
              children: a
            }), i[a].mediaItemsWithErrors.map((n, s) => l(N, {
              type: w.textHelp,
              fontWeight: He.bold,
              children: n
            }, s))]
          }, r))
        }),
        type: "danger"
      });
    } else
      H.open(l(ze, {
        mediaItems: t,
        mediaItemsErrors: i,
        close: () => {
        }
      }), {
        maxWidth: "640px",
        modalPosition: "center"
      });
  };
  return (!u || u.length === 0) && !O ? null : v("div", {
    className: D("gallery-container"),
    children: [l(Re, {
      ...h
    }), v("div", {
      className: `gallery-list ${B}`,
      children: [l(Le, {
        multiple: !_ || _ > 1,
        accept: x,
        isOpen: re,
        mediaItemTransformers: [J, Y],
        mediaItemProcessors: [j, Z, $, q],
        mediaItemValidators: [Q, K, ie, ee, te, oe],
        onFilesSelected: le,
        openCallback: F
      }), f.map((t, i) => {
        var o;
        return l(Ge, {
          position: "top",
          message: (o = t == null ? void 0 : t.settings) != null && o.toBeDeleted ? "This media item has been marked for deletion and will be removed once all directories are synced." : "",
          children: v("div", {
            className: D(Oe, Te, Me),
            children: [l(Be, {
              format: p == null ? void 0 : p.format,
              mediaItem: t,
              actions: ce(t)
            }, t.uuid ?? i), l(ke, {
              if: !!t.mediaItemLabel,
              children: l(N, {
                type: w.textCaption,
                children: t.mediaItemLabel
              })
            })]
          })
        }, t.uuid ?? i);
      }), l(Ue, {
        icons: U,
        format: p == null ? void 0 : p.format,
        text: p == null ? void 0 : p.text,
        visible: O && f.length < _,
        onClick: () => {
          P({
            ...h,
            title: "",
            description: ""
          }), F();
        }
      })]
    })]
  });
};
export {
  vt as GALLERY_PHOTO_SCOPE,
  gt as Gallery
};
