"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _uploadHelper = require("./upload-helper");
var _dropdownItemTitle = _interopRequireDefault(require("./dropdown-item-title"));
var _useTreeNodeNavigation = require("../hooks/use-tree-navigation/use-tree-node-navigation");
var _utils = require("../hooks/use-tree-navigation/utils");
var _dropdownItemTooltip = _interopRequireDefault(require("./dropdown-item-tooltip"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const isFolderUploadSupported = (0, _uploadHelper.supportsFolderUpload)();
const DropdownItemFileUpload = ({
  option,
  onSelect = () => {},
  onCloseDropdown = () => {}
}) => {
  const {
    disabled,
    id,
    testId,
    acceptedFiles = [],
    isMultiple = false,
    isFolderUpload = false
  } = option;
  const accept = acceptedFiles ? acceptedFiles.join(', ') : '*';
  const labelRef = (0, _react.useRef)(null);
  const handleFileSelection = e => {
    if (typeof e === 'boolean') {
      labelRef.current?.click();
    } else {
      e?.preventDefault?.();
      if (e?.target?.files) {
        onSelect(option, {
          selectedFiles: e.target.files
        });
      }
    }
  };
  const {
    treeItemAttrs,
    ref: setRefCallback
  } = (0, _useTreeNodeNavigation.useTreeNodeNavigation)({
    id: option.privateKey || option.key,
    getKeyActionFn: _utils.getKeyActionFnMenu,
    onCustomButtonKeyDown: onCloseDropdown,
    onToggleSelected: handleFileSelection,
    ignoreKeys: null
  });
  if (isFolderUpload && !isFolderUploadSupported) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemTooltip.default, {
    option: option,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("label", {
      "test-id": testId,
      htmlFor: id,
      className: (0, _classnames.default)('drop-file-upload-label truncated-drop-item', {
        disabled
      }),
      ref: labelRef,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemTitle.default, {
        option: option
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
        className: "drop-file-upload-input",
        "test-id": `drop-file-upload-input-${id}`,
        ref: setRefCallback,
        type: "file",
        id: id,
        disabled: disabled,
        onChange: handleFileSelection,
        accept: accept,
        multiple: isMultiple
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        ,
        webkitdirectory: isFolderUpload && isFolderUpload.toString() || null,
        onKeyDown: treeItemAttrs.onKeyDown
      })]
    })
  });
};
var _default = exports.default = DropdownItemFileUpload;