"use strict";

var __assign = void 0 && (void 0).__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);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

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

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PreviewLisMode = exports.PreviewTheme = exports.PreviewType = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var ReactDOM = __importStar(require("react-dom"));

var useKey_1 = __importDefault(require("react-use/lib/useKey"));

var classnames_1 = __importDefault(require("classnames"));

var react_virtualized_1 = require("react-virtualized");

var useMeasure_1 = __importDefault(require("react-use/lib/useMeasure"));

var useDataValue_1 = __importDefault(require("../../hooks/useDataValue"));

var useStaticCallback_1 = __importDefault(require("../../hooks/useStaticCallback"));

var scrollbar_1 = require("../scrollbar");

var icon_1 = require("../icon");

require("./style/index.css");

var react_1 = require("react");

var button_1 = require("../button");

var tooltip_1 = require("../tooltip");

var PreviewType;

(function (PreviewType) {
  PreviewType["normal"] = "normal";
  PreviewType["fullscreen"] = "fullscreen";
})(PreviewType = exports.PreviewType || (exports.PreviewType = {}));

var PreviewTheme;

(function (PreviewTheme) {
  PreviewTheme["initial"] = "initial"; // 全屏主题

  PreviewTheme["defaultFull"] = "default-full";
  PreviewTheme["blackFull"] = "black-full";
})(PreviewTheme = exports.PreviewTheme || (exports.PreviewTheme = {}));

var PreviewLisMode;

(function (PreviewLisMode) {
  PreviewLisMode["media"] = "media";
  PreviewLisMode["other"] = "other";
})(PreviewLisMode = exports.PreviewLisMode || (exports.PreviewLisMode = {}));

var prefixCls = 'dmc-preview-list';
var scrollbarOpts = {
  suppressScrollY: true,
  minScrollbarLength: 20
};

var PreviewList = function PreviewList(props) {
  var _a;

  var _b;

  var list = props.list,
      _c = props.mode,
      mode = _c === void 0 ? PreviewLisMode.other : _c,
      selectIndex = props.selectIndex,
      defaultSelectIndex = props.defaultSelectIndex,
      className = props.className,
      RenderCell = props.RenderCell,
      RenderPreview = props.RenderPreview,
      onChangeSelect = props.onChangeSelect,
      type = props.type,
      showList = props.showList,
      showEdit = props.showEdit,
      showShare = props.showShare,
      title = props.title,
      _d = props.theme,
      theme = _d === void 0 ? PreviewTheme.initial : _d,
      handleClose = props.handleClose,
      handleEdit = props.handleEdit,
      handleShare = props.handleShare,
      _e = props.shareTip,
      shareTip = _e === void 0 ? '' : _e,
      renderAction = props.renderAction,
      onScroll = props.onScroll,
      height = props.height,
      rowHeight = props.rowHeight,
      columnWidth = props.columnWidth,
      restProps = __rest(props, ["list", "mode", "selectIndex", "defaultSelectIndex", "className", "RenderCell", "RenderPreview", "onChangeSelect", "type", "showList", "showEdit", "showShare", "title", "theme", "handleClose", "handleEdit", "handleShare", "shareTip", "renderAction", "onScroll", "height", "rowHeight", "columnWidth"]);

  var _f = useMeasure_1["default"](),
      ref = _f[0],
      width = _f[1].width;

  var listLen = list.length;
  var gridRef = React.useRef(null);
  var psRef = React.useRef();

  var _g = React.useState(defaultSelectIndex !== null && defaultSelectIndex !== void 0 ? defaultSelectIndex : 0),
      unControlVal = _g[0],
      setUnControlVal = _g[1];

  var isControl = ('selectIndex' in props);
  var val = isControl ? selectIndex !== null && selectIndex !== void 0 ? selectIndex : 0 : unControlVal;
  var itemCls = prefixCls + "-item";
  var itemInnerCls = prefixCls + "-item-inner";
  var itemSelectCls = prefixCls + "-item-active";

  var _title = title || ((_b = list === null || list === void 0 ? void 0 : list[val]) === null || _b === void 0 ? void 0 : _b.name);

  var clickItem = useDataValue_1["default"](function (valStr) {
    var index = parseInt(valStr);
    !isControl && setUnControlVal(index);
    onChangeSelect && onChangeSelect(index);
  });
  var clickPreNav = useStaticCallback_1["default"](function () {
    var index = val - 1;
    !isControl && setUnControlVal(index);
    onChangeSelect && onChangeSelect(index);
  });
  var clickNextNav = useStaticCallback_1["default"](function () {
    var index = val + 1;
    !isControl && setUnControlVal(index);
    onChangeSelect && onChangeSelect(index);
  });
  var clickKey = useStaticCallback_1["default"](function (event) {
    var index = val;

    if (event.key === 'ArrowLeft' && val > 0) {
      index = val - 1;
    } else if (event.key === 'ArrowRight' && val < listLen - 1) {
      index = val + 1;
    }

    if (index !== val) {
      !isControl && setUnControlVal(index);
      onChangeSelect && onChangeSelect(index);
    }
  });
  var keyFilter = react_1.useCallback(function (event) {
    return event.key === 'ArrowRight' || event.key === 'ArrowLeft';
  }, []);
  useKey_1["default"](keyFilter, clickKey, undefined, [clickKey]);

  var cellRenderer = function cellRenderer(_a) {
    var _b;

    var isVisible = _a.isVisible,
        columnIndex = _a.columnIndex,
        style = _a.style;

    if (!isVisible || columnIndex >= listLen) {
      return null;
    }

    var data = list[columnIndex];
    var cls = classnames_1["default"](itemCls, (_b = {}, _b[itemSelectCls] = columnIndex === val, _b));
    return jsx_runtime_1.jsx("div", __assign({
      style: style,
      className: cls
    }, {
      children: jsx_runtime_1.jsx("div", __assign({
        "data-value": columnIndex,
        className: itemInnerCls,
        onClick: clickItem
      }, {
        children: jsx_runtime_1.jsx(RenderCell, {
          item: data
        }, void 0)
      }), void 0)
    }), columnIndex);
  };

  React.useEffect(function () {
    var dom = ReactDOM.findDOMNode(gridRef.current);

    if (dom) {
      var _a = scrollbar_1.createScrollbar(dom, 'assist-bg', scrollbarOpts),
          ps = _a[0],
          destroy_1 = _a[1];

      psRef.current = ps;
      return function () {
        destroy_1();
        psRef.current = undefined;
      };
    }
  }, []);
  React.useEffect(function () {
    psRef.current && psRef.current.update();
  });
  return jsx_runtime_1.jsxs("div", __assign({}, restProps, {
    className: classnames_1["default"](prefixCls, className, prefixCls + "-" + theme, (_a = {}, _a[prefixCls + "-fullscreen"] = type === PreviewType.fullscreen, _a)),
    ref: ref
  }, {
    children: [jsx_runtime_1.jsxs("div", __assign({
      className: prefixCls + "-preview"
    }, {
      children: [type === PreviewType.fullscreen && jsx_runtime_1.jsxs("div", __assign({
        className: prefixCls + "-head " + prefixCls + "-head-" + theme
      }, {
        children: [jsx_runtime_1.jsxs("div", __assign({
          className: "name"
        }, {
          children: [jsx_runtime_1.jsx("div", {
            children: _title
          }, void 0), showShare && jsx_runtime_1.jsx(tooltip_1.Tooltip, __assign({
            title: shareTip
          }, {
            children: jsx_runtime_1.jsx(button_1.IconButton, {
              type: "only-icon",
              icon: "launch-2",
              onClick: function onClick() {
                handleShare && handleShare();
              }
            }, void 0)
          }), void 0)]
        }), void 0), jsx_runtime_1.jsxs("div", __assign({
          className: prefixCls + "-action"
        }, {
          children: [renderAction && renderAction(val), !renderAction && showEdit && jsx_runtime_1.jsx(button_1.IconButton, {
            type: "only-icon",
            icon: "edit",
            onClick: function onClick() {
              var _a;

              if (handleEdit === null || handleEdit === void 0 ? void 0 : handleEdit.length) {
                if (mode === PreviewLisMode.media && list[val].type === 'image') {
                  (_a = handleEdit[1]) === null || _a === void 0 ? void 0 : _a.call(handleEdit);
                } else {
                  handleEdit[0]();
                }
              }

              handleClose && handleClose();
            }
          }, void 0), jsx_runtime_1.jsx(button_1.IconButton, {
            type: "only-icon",
            icon: "close",
            onClick: function onClick() {
              return handleClose && handleClose();
            }
          }, void 0)]
        }), void 0)]
      }), void 0), type === PreviewType.fullscreen ? jsx_runtime_1.jsxs("div", __assign({
        className: prefixCls + "-preview-container"
      }, {
        children: [jsx_runtime_1.jsx("div", {
          children: val !== 0 && jsx_runtime_1.jsx("div", __assign({
            className: prefixCls + "-nav " + prefixCls + "-left-nav",
            onClick: clickPreNav
          }, {
            children: jsx_runtime_1.jsx(icon_1.Icon, {
              icon: "chevron-left"
            }, void 0)
          }), void 0)
        }, void 0), jsx_runtime_1.jsx("div", __assign({
          className: "content"
        }, {
          children: jsx_runtime_1.jsx(RenderPreview, {
            item: list[val],
            handleClose: handleClose
          }, void 0)
        }), void 0), jsx_runtime_1.jsx("div", {
          children: val < listLen - 1 && jsx_runtime_1.jsx("div", __assign({
            className: prefixCls + "-nav " + prefixCls + "-right-nav",
            onClick: clickNextNav
          }, {
            children: jsx_runtime_1.jsx(icon_1.Icon, {
              icon: "chevron-right"
            }, void 0)
          }), void 0)
        }, void 0)]
      }), void 0) : jsx_runtime_1.jsxs(jsx_runtime_1.Fragment, {
        children: [jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-preview-container"
        }, {
          children: jsx_runtime_1.jsx(RenderPreview, {
            item: list[val],
            handleClose: handleClose
          }, void 0)
        }), void 0), val !== 0 && jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-nav " + prefixCls + "-left-nav",
          onClick: clickPreNav
        }, {
          children: jsx_runtime_1.jsx(icon_1.Icon, {
            icon: "chevron-left"
          }, void 0)
        }), void 0), val < listLen - 1 && jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-nav " + prefixCls + "-right-nav",
          onClick: clickNextNav
        }, {
          children: jsx_runtime_1.jsx(icon_1.Icon, {
            icon: "chevron-right"
          }, void 0)
        }), void 0)]
      }, void 0)]
    }), void 0), showList && jsx_runtime_1.jsx(react_virtualized_1.Grid, {
      ref: gridRef,
      width: width,
      height: height || 78,
      rowHeight: rowHeight || 64,
      rowCount: 1,
      cellRenderer: cellRenderer,
      columnWidth: columnWidth || 116,
      columnCount: listLen,
      scrollToColumn: val,
      className: prefixCls + "-list",
      onScroll: onScroll
    }, void 0)]
  }), void 0);
};

PreviewList.defaultProps = {
  type: PreviewType.normal,
  showList: true,
  showEdit: false,
  showShare: false,
  title: '',
  shareTip: ''
};
exports["default"] = PreviewList;

