"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }


var _chunkAEJ7VTL4cjs = require('./chunk-AEJ7VTL4.cjs');


var _chunkL2UOQCGRcjs = require('./chunk-L2UOQCGR.cjs');


var _chunkWF7Z2VITcjs = require('./chunk-WF7Z2VIT.cjs');

// src/components/FileInput.tsx
var _styled = require('@emotion/styled'); var _styled2 = _interopRequireDefault(_styled);
var _react = require('react');
var _jsxruntime = require('@emotion/react/jsx-runtime');
function FileInput({
  name,
  onChange,
  filePurpose = "",
  onRemoveFile
}) {
  const [fileName, setFileName] = _react.useState.call(void 0, );
  const [fileContent, setFileContent] = _react.useState.call(void 0, );
  const [fileErrorMsg, setFileErrorMsg] = _react.useState.call(void 0, );
  const fileInputRef = _react.useRef.call(void 0, null);
  const handleChange = (changeEvent) => {
    var _a;
    const file = (_a = changeEvent.target.files) == null ? void 0 : _a[0];
    if (!file) {
      return;
    }
    const reader = new FileReader();
    reader.onload = (readEvent) => {
      var _a2;
      let fileContent2 = (_a2 = readEvent == null ? void 0 : readEvent.target) == null ? void 0 : _a2.result;
      setFileName(file.name);
      if (!fileContent2) {
        setFileErrorMsg("Unable to read file");
        return;
      }
      if (typeof fileContent2 !== "string") {
        fileContent2 = new TextDecoder().decode(fileContent2);
      }
      setFileErrorMsg(void 0);
      setFileContent(fileContent2);
      onChange(fileContent2);
    };
    reader.readAsText(file);
  };
  function handleRemoveFile() {
    if (fileInputRef.current) {
      fileInputRef.current.value = "";
    }
    setFileName(void 0);
    setFileContent(void 0);
    setFileErrorMsg(void 0);
    onRemoveFile();
  }
  return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, StyledFileInputContainer, { children: [
    /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, StyledFileInput, { hasValidFileSet: Boolean(fileName && fileContent), children: [
      /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "label", { htmlFor: name, children: fileName && fileContent ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        _chunkL2UOQCGRcjs.Pill,
        {
          text: fileName,
          cursor: "pointer",
          onDeleteMouseDown: handleRemoveFile,
          onDeleteFromKeyboard: handleRemoveFile
        }
      ) : /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.Fragment, { children: [
        /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkWF7Z2VITcjs.Icon, { name: "Upload", width: 26, tutorialStep: 6 }),
        "Upload ",
        filePurpose || "",
        " file"
      ] }) }),
      /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        "input",
        {
          type: "file",
          id: name,
          onChange: handleChange,
          ref: fileInputRef
        }
      )
    ] }),
    fileErrorMsg ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkAEJ7VTL4cjs.InputSubtext, { text: fileErrorMsg, hasError: true }) : null
  ] });
}
var StyledFileInputContainer = _styled2.default.div``;
var StyledFileInput = _styled2.default.div`
  input[type="file"] {
    display: none;
  }

  label {
    display: block;
    cursor: pointer;
    ${({ theme, hasValidFileSet }) => _chunkAEJ7VTL4cjs.inputBlockStyle.call(void 0, { theme, hasValue: hasValidFileSet, hasError: false })}
  }
`;




exports.FileInput = FileInput; exports.StyledFileInput = StyledFileInput;
