var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from 'react';
import "./index.css";
import { dialogApi } from "../dialog";
import PreviewList, { PreviewType } from "../preview-list";
import { getTheme } from "../style/themes";
import videoCamDark from "../map-info-window/asserts/video-cam-dark.svg";
import videoCamLight from "../map-info-window/asserts/video-cam-light.svg";
import pdfSvg from "../map-info-window/asserts/pdf.svg";
import { useMount } from 'ahooks';
export var imgTypes = ['png', 'jpg', 'jpeg', 'webp', 'avif'];
export var videoTypes = ['mp4', 'flv', '3gp', 'mpeg', 'ogg', 'webm'];
export var pdfTypes = ['pdf', 'PDF'];
var prefixCls = 'dmc';

var Player = function Player(props) {
  var url = props.url,
      prefixCls = props.prefixCls;
  var ref = React.useRef(null);
  useMount(function () {
    import('xgplayer').then(function (_a) {
      var Player = _a["default"];
      new Player({
        el: ref.current,
        autoplay: true,
        url: url,
        fitVideoSize: 'auto'
      });
    });
  });
  return _jsx("div", __assign({
    className: prefixCls + "-video-content"
  }, {
    children: _jsx("div", {
      ref: ref
    }, void 0)
  }), void 0);
};

var RenderCell = function RenderCell(props) {
  var _a, _b, _c, _d;

  var theme = getTheme() || 'dark';
  var img = videoCamDark;

  if (imgTypes.includes((_a = props === null || props === void 0 ? void 0 : props.item) === null || _a === void 0 ? void 0 : _a.type)) {
    img = (_b = props === null || props === void 0 ? void 0 : props.item) === null || _b === void 0 ? void 0 : _b.value;
  } else if (videoTypes.includes((_c = props === null || props === void 0 ? void 0 : props.item) === null || _c === void 0 ? void 0 : _c.type)) {
    img = theme === 'dark' ? videoCamDark : videoCamLight;
  } else if (pdfTypes.includes((_d = props === null || props === void 0 ? void 0 : props.item) === null || _d === void 0 ? void 0 : _d.type)) {
    img = pdfSvg;
  }

  return _jsx("div", {
    className: prefixCls + "-media-preview-list-item",
    style: {
      backgroundPosition: 'center',
      backgroundSize: 'contain',
      backgroundRepeat: 'no-repeat',
      backgroundImage: "url(" + img + ")",
      height: '100%'
    }
  }, void 0);
};

var RenderPreview = function RenderPreview(props) {
  var _a, _b, _c, _d, _e, _f;

  var _element = _jsxs("div", {
    children: [" ", (_a = props === null || props === void 0 ? void 0 : props.item) === null || _a === void 0 ? void 0 : _a.name]
  }, void 0);

  var element = _jsxs("div", __assign({
    className: prefixCls + "-media-preview-item"
  }, {
    children: [_element, _jsx("div", {
      style: {
        width: '100%',
        height: 'calc(100% - 20px)',
        backgroundPosition: 'center',
        backgroundSize: 'contain',
        backgroundRepeat: 'no-repeat',
        backgroundImage: "url(" + ((_b = props === null || props === void 0 ? void 0 : props.item) === null || _b === void 0 ? void 0 : _b.value) + ")"
      }
    }, void 0)]
  }), void 0);

  if (videoTypes.includes((_c = props === null || props === void 0 ? void 0 : props.item) === null || _c === void 0 ? void 0 : _c.type)) {
    element = _jsxs("div", __assign({
      className: prefixCls + "-media-preview-item"
    }, {
      children: [_element, _jsx(Player, {
        url: (_d = props === null || props === void 0 ? void 0 : props.item) === null || _d === void 0 ? void 0 : _d.value,
        id: 'video_' + Math.random(),
        prefixCls: prefixCls
      }, void 0)]
    }), void 0);
  }

  if (pdfTypes.includes((_e = props === null || props === void 0 ? void 0 : props.item) === null || _e === void 0 ? void 0 : _e.type)) {
    element = _jsxs("div", __assign({
      className: prefixCls + "-media-preview-item"
    }, {
      children: [_element, _jsx("iframe", {
        title: '',
        src: (_f = props === null || props === void 0 ? void 0 : props.item) === null || _f === void 0 ? void 0 : _f.value,
        width: "100%",
        height: "100%"
      }, void 0)]
    }), void 0);
  }

  return element;
};

var openMediaPreviewDialog = function openMediaPreviewDialog(props) {
  var _a = props.list,
      list = _a === void 0 ? [] : _a,
      getPreviewPopupContainer = props.getPreviewPopupContainer,
      _b = props.defaultSelectIndex,
      defaultSelectIndex = _b === void 0 ? 0 : _b;
  dialogApi.open({
    closable: true,
    maskClosable: true,
    mask: true,
    wrapClassName: prefixCls + "-media-preview",
    style: {
      width: '100vw',
      height: '100vh',
      margin: 0
    },
    getContainer: getPreviewPopupContainer,
    footer: function footer() {
      return null;
    }
  }, function (onClose) {
    return _jsx(PreviewList, {
      title: "preview",
      defaultSelectIndex: defaultSelectIndex,
      handleClose: onClose,
      showEdit: false,
      list: list,
      type: PreviewType.normal,
      RenderCell: RenderCell,
      RenderPreview: RenderPreview
    }, void 0);
  });
};

export default openMediaPreviewDialog;

