"use strict";
const vue = require("vue");
const index = require("./index-C_bsiScj.js");
const useDefaults = require("./useDefaults-YXpmugKp.js");
const useId = require("./useId-Dl8Hi5_U.js");
const useStateClass = require("./useStateClass-Bk6-veG2.js");
const dom = require("./dom-BgEUZpKg.js");
const _hoisted_1 = ["for"];
const _hoisted_2 = ["aria-disabled"];
const _hoisted_3 = ["id", "disabled", "aria-label", "aria-labelledby"];
const _hoisted_4 = { class: "b-form-file-text" };
const _hoisted_5 = { key: 0 };
const _hoisted_6 = {
  key: 1,
  class: "text-muted"
};
const _hoisted_7 = {
  key: 0,
  class: "b-form-file-drag-overlay"
};
const _hoisted_8 = { class: "b-form-file-drag-text" };
const _hoisted_9 = ["name", "form", "multiple", "disabled", "required", "accept", "capture", "directory", "webkitdirectory"];
const _hoisted_10 = ["id", "form", "name", "multiple", "disabled", "capture", "accept", "required", "aria-label", "aria-labelledby", "aria-required", "directory", "webkitdirectory"];
const _hoisted_11 = {
  key: 3,
  class: "b-form-file-display mt-2"
};
const _hoisted_12 = {
  key: 0,
  class: "small text-muted"
};
const _hoisted_13 = {
  key: 1,
  class: "small text-muted"
};
const _hoisted_14 = {
  key: 4,
  class: "visually-hidden",
  "aria-live": "polite",
  "aria-atomic": "true"
};
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    inheritAttrs: false
  },
  __name: "BFormFile",
  props: /* @__PURE__ */ vue.mergeModels({
    ariaLabel: { default: void 0 },
    ariaLabelledby: { default: void 0 },
    accept: { default: "" },
    autofocus: { type: Boolean, default: false },
    browseText: { default: void 0 },
    capture: { default: void 0 },
    directory: { type: Boolean, default: false },
    disabled: { type: Boolean, default: false },
    dropPlaceholder: { default: void 0 },
    fileNameFormatter: { type: Function, default: void 0 },
    form: { default: void 0 },
    id: { default: void 0 },
    label: { default: "" },
    labelClass: { default: void 0 },
    multiple: { type: Boolean, default: false },
    name: { default: void 0 },
    noButton: { type: Boolean, default: false },
    noDrop: { type: Boolean, default: false },
    plain: { type: Boolean, default: false },
    placeholder: { default: "No file chosen" },
    required: { type: Boolean, default: false },
    showFileNames: { type: Boolean, default: false },
    size: { default: void 0 },
    state: { type: [Boolean, null], default: null }
  }, {
    "modelValue": {
      default: null
    },
    "modelModifiers": {}
  }),
  emits: /* @__PURE__ */ vue.mergeModels(["change"], ["update:modelValue"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const _props = __props;
    const props = useDefaults.useDefaults(_props, "BFormFile");
    const slots = vue.useSlots();
    const emit = __emit;
    const modelValue = vue.useModel(__props, "modelValue");
    const attrs = vue.useAttrs();
    const processedAttrs = vue.computed(() => {
      if (props.plain) {
        return {
          rootAttrs: {},
          dropZoneAttrs: {},
          inputAttrs: attrs
        };
      }
      const { class: rootClass, style: rootStyle, title: dropZoneTitle, ...inputAttrs } = attrs;
      const rootAttrs = {};
      const dropZoneAttrs = {};
      if (rootClass !== void 0) rootAttrs.class = rootClass;
      if (rootStyle !== void 0) rootAttrs.style = rootStyle;
      if (dropZoneTitle !== void 0) dropZoneAttrs.title = dropZoneTitle;
      return {
        rootAttrs,
        dropZoneAttrs,
        inputAttrs
      };
    });
    const computedId = useId.useId(() => props.id);
    const stateClass = useStateClass.useStateClass(() => props.state);
    const rootRef = vue.useTemplateRef("rootRef");
    const dropZoneRef = vue.useTemplateRef("dropZoneRef");
    const browseButtonRef = vue.useTemplateRef("browseButtonRef");
    const plainInputRef = vue.useTemplateRef("plainInputRef");
    const customInputRef = vue.useTemplateRef("customInputRef");
    const computedAccept = vue.computed(
      () => typeof props.accept === "string" ? props.accept : props.accept.join(",")
    );
    const {
      open,
      reset: resetDialog,
      onChange: onDialogChange
    } = index.useFileDialog({
      accept: computedAccept.value,
      multiple: props.multiple || props.directory,
      directory: props.directory,
      input: customInputRef
    });
    const { isOverDropZone } = index.useDropZone(dropZoneRef, {
      onDrop: (files) => {
        if (files && !props.noDrop) {
          handleFiles(files);
        }
      },
      multiple: props.multiple || props.directory
    });
    const hasLabelSlot = vue.computed(() => !dom.isEmptySlot(slots.label));
    const hasPlaceholderSlot = vue.computed(() => !dom.isEmptySlot(slots.placeholder));
    const computedClasses = vue.computed(() => [
      stateClass.value,
      {
        [`form-control-${props.size}`]: props.size !== void 0
      }
    ]);
    const computedPlainClasses = vue.computed(() => [
      "form-control",
      stateClass.value,
      {
        [`form-control-${props.size}`]: props.size !== void 0
      }
    ]);
    const internalFiles = vue.ref([]);
    const selectedFiles = vue.computed(() => internalFiles.value);
    const hasFiles = vue.computed(() => selectedFiles.value.length > 0);
    const fileNames = vue.computed(() => selectedFiles.value.map((file) => file.name));
    const formattedFileNames = vue.computed(() => {
      if (!hasFiles.value) return "";
      if (props.fileNameFormatter) {
        return props.fileNameFormatter(selectedFiles.value);
      }
      const names = fileNames.value;
      if (names.length === 1) return names[0];
      return `${names.length} files selected`;
    });
    const showExternalDisplay = vue.computed(
      () => !props.plain && props.showFileNames && (hasFiles.value || props.placeholder)
    );
    const ariaLiveMessage = vue.computed(() => {
      if (!hasFiles.value) return "";
      const count = selectedFiles.value.length;
      if (count === 1) {
        return `File selected: ${selectedFiles.value[0].name}`;
      }
      return `${count} files selected`;
    });
    const effectiveBrowseText = vue.computed(() => props.browseText ?? "Browse");
    const effectiveDropPlaceholder = vue.computed(() => props.dropPlaceholder ?? "Drop files here...");
    const isFileAccepted = (file) => {
      if (!computedAccept.value) return true;
      const acceptTypes = computedAccept.value.split(",").map((type) => type.trim());
      return acceptTypes.some((acceptType) => {
        if (acceptType.startsWith(".")) {
          return file.name.toLowerCase().endsWith(acceptType.toLowerCase());
        }
        if (!acceptType.includes("*")) {
          return file.type === acceptType;
        }
        const slashIndex = acceptType.indexOf("/");
        if (slashIndex === -1) {
          return false;
        }
        const category = acceptType.slice(0, slashIndex);
        if (category === "*") {
          return true;
        }
        return file.type.startsWith(`${category}/`);
      });
    };
    const handleFiles = (files, nativeEvent) => {
      let fileArray = [];
      if (nativeEvent) {
        const input = nativeEvent.target;
        fileArray = input.files ? Array.from(input.files) : [];
      } else {
        fileArray = Array.from(files).filter((file) => isFileAccepted(file));
        if (customInputRef.value && typeof DataTransfer !== "undefined") {
          try {
            const dataTransfer = new DataTransfer();
            fileArray.forEach((file) => dataTransfer.items.add(file));
            customInputRef.value.files = dataTransfer.files;
          } catch {
          }
        }
      }
      internalFiles.value = fileArray;
      if (fileArray.length === 0) {
        modelValue.value = null;
      } else if (props.directory || props.multiple) {
        modelValue.value = fileArray;
      } else {
        const [firstFile] = fileArray;
        modelValue.value = firstFile;
      }
      vue.nextTick(() => {
        if (nativeEvent) {
          emit("change", nativeEvent);
        } else {
          const changeEvent = new CustomEvent("change", {
            bubbles: true,
            cancelable: false,
            detail: {
              files: fileArray,
              target: { files: fileArray }
            }
          });
          Object.defineProperty(changeEvent, "files", {
            value: fileArray,
            enumerable: true
          });
          emit("change", changeEvent);
        }
      });
    };
    const openFileDialog = () => {
      if (!props.disabled) {
        open({
          accept: computedAccept.value,
          multiple: props.multiple || props.directory,
          directory: props.directory
        });
      }
    };
    const handleControlClick = () => {
      if (!props.disabled) {
        openFileDialog();
      }
    };
    const onPlainChange = (e) => {
      const input = e.target;
      if (input.files) {
        handleFiles(input.files, e);
      }
    };
    onDialogChange((files) => {
      if (files) {
        handleFiles(files);
      }
    });
    const reset = () => {
      internalFiles.value = [];
      modelValue.value = null;
      resetDialog();
      if (plainInputRef.value) {
        plainInputRef.value.value = "";
      }
    };
    const focus = () => {
      if (props.plain) {
        plainInputRef.value?.focus();
      } else {
        browseButtonRef.value?.focus();
      }
    };
    const blur = () => {
      if (props.plain) {
        plainInputRef.value?.blur();
      } else {
        browseButtonRef.value?.blur();
      }
    };
    vue.onMounted(() => {
      if (props.autofocus) {
        vue.nextTick(() => {
          focus();
        });
      }
    });
    vue.watch(
      () => props.autofocus,
      (autofocus) => {
        if (autofocus) {
          focus();
        }
      }
    );
    vue.watch(modelValue, (newValue) => {
      if (newValue === null) {
        internalFiles.value = [];
        if (plainInputRef.value) {
          plainInputRef.value.value = "";
        }
      } else if (Array.isArray(newValue)) {
        internalFiles.value = newValue;
      } else {
        internalFiles.value = [newValue];
      }
    });
    __expose({
      blur,
      element: vue.computed(() => props.plain ? plainInputRef.value : browseButtonRef.value),
      focus,
      reset
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", vue.mergeProps({
        ref_key: "rootRef",
        ref: rootRef
      }, processedAttrs.value.rootAttrs, { class: "b-form-file-root" }), [
        hasLabelSlot.value || vue.unref(props).label ? (vue.openBlock(), vue.createElementBlock("label", {
          key: 0,
          class: vue.normalizeClass(["form-label", vue.unref(props).labelClass]),
          for: vue.unref(computedId)
        }, [
          vue.renderSlot(_ctx.$slots, "label", {}, () => [
            vue.createTextVNode(vue.toDisplayString(vue.unref(props).label), 1)
          ])
        ], 10, _hoisted_1)) : vue.createCommentVNode("", true),
        !vue.unref(props).plain ? (vue.openBlock(), vue.createElementBlock("div", vue.mergeProps({
          key: 1,
          ref_key: "dropZoneRef",
          ref: dropZoneRef
        }, processedAttrs.value.dropZoneAttrs, {
          class: ["b-form-file-wrapper", {
            "b-form-file-dragging": vue.unref(isOverDropZone) && !vue.unref(props).noDrop,
            "b-form-file-has-files": hasFiles.value
          }]
        }), [
          vue.createElementVNode("div", {
            class: vue.normalizeClass(["b-form-file-control", computedClasses.value]),
            "aria-disabled": vue.unref(props).disabled,
            onClick: handleControlClick
          }, [
            !vue.unref(props).noButton ? (vue.openBlock(), vue.createElementBlock("button", {
              key: 0,
              id: vue.unref(computedId),
              ref_key: "browseButtonRef",
              ref: browseButtonRef,
              type: "button",
              class: "b-form-file-button",
              disabled: vue.unref(props).disabled,
              "aria-label": vue.unref(props).ariaLabel,
              "aria-labelledby": vue.unref(props).ariaLabelledby,
              onClick: vue.withModifiers(openFileDialog, ["stop"])
            }, vue.toDisplayString(effectiveBrowseText.value), 9, _hoisted_3)) : vue.createCommentVNode("", true),
            vue.createElementVNode("div", _hoisted_4, [
              vue.renderSlot(_ctx.$slots, "file-name", {
                files: selectedFiles.value,
                names: fileNames.value
              }, () => [
                hasFiles.value ? (vue.openBlock(), vue.createElementBlock("span", _hoisted_5, vue.toDisplayString(formattedFileNames.value), 1)) : hasPlaceholderSlot.value || vue.unref(props).placeholder ? (vue.openBlock(), vue.createElementBlock("span", _hoisted_6, [
                  vue.renderSlot(_ctx.$slots, "placeholder", {}, () => [
                    vue.createTextVNode(vue.toDisplayString(vue.unref(props).placeholder), 1)
                  ])
                ])) : vue.createCommentVNode("", true)
              ])
            ])
          ], 10, _hoisted_2),
          vue.unref(isOverDropZone) && !vue.unref(props).noDrop ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_7, [
            vue.renderSlot(_ctx.$slots, "drop-placeholder", {}, () => [
              vue.createElementVNode("div", _hoisted_8, vue.toDisplayString(effectiveDropPlaceholder.value), 1)
            ])
          ])) : vue.createCommentVNode("", true),
          vue.createElementVNode("input", vue.mergeProps({
            ref_key: "customInputRef",
            ref: customInputRef
          }, processedAttrs.value.inputAttrs, {
            type: "file",
            name: vue.unref(props).name,
            form: vue.unref(props).form,
            multiple: vue.unref(props).multiple || vue.unref(props).directory,
            disabled: vue.unref(props).disabled,
            required: vue.unref(props).required,
            accept: computedAccept.value || void 0,
            capture: vue.unref(props).capture,
            directory: vue.unref(props).directory || void 0,
            webkitdirectory: vue.unref(props).directory || void 0,
            tabindex: "-1",
            "aria-hidden": "true",
            style: { "position": "absolute", "z-index": "-5", "width": "0", "height": "0", "opacity": "0", "overflow": "hidden", "pointer-events": "none" }
          }), null, 16, _hoisted_9)
        ], 16)) : (vue.openBlock(), vue.createElementBlock("input", vue.mergeProps({
          key: 2,
          id: vue.unref(computedId),
          ref_key: "plainInputRef",
          ref: plainInputRef
        }, processedAttrs.value.inputAttrs, {
          type: "file",
          class: computedPlainClasses.value,
          form: vue.unref(props).form,
          name: vue.unref(props).name,
          multiple: vue.unref(props).multiple || vue.unref(props).directory,
          disabled: vue.unref(props).disabled,
          capture: vue.unref(props).capture,
          accept: computedAccept.value || void 0,
          required: vue.unref(props).required || void 0,
          "aria-label": vue.unref(props).ariaLabel,
          "aria-labelledby": vue.unref(props).ariaLabelledby,
          "aria-required": vue.unref(props).required || void 0,
          directory: vue.unref(props).directory || void 0,
          webkitdirectory: vue.unref(props).directory || void 0,
          onChange: onPlainChange
        }), null, 16, _hoisted_10)),
        showExternalDisplay.value ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_11, [
          vue.renderSlot(_ctx.$slots, "file-name", {
            files: selectedFiles.value,
            names: fileNames.value
          }, () => [
            hasFiles.value ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_12, vue.toDisplayString(formattedFileNames.value), 1)) : hasPlaceholderSlot.value || vue.unref(props).placeholder ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_13, [
              vue.renderSlot(_ctx.$slots, "placeholder", {}, () => [
                vue.createTextVNode(vue.toDisplayString(vue.unref(props).placeholder), 1)
              ])
            ])) : vue.createCommentVNode("", true)
          ])
        ])) : vue.createCommentVNode("", true),
        !vue.unref(props).plain ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_14, vue.toDisplayString(ariaLiveMessage.value), 1)) : vue.createCommentVNode("", true)
      ], 16);
    };
  }
});
exports._sfc_main = _sfc_main;
//# sourceMappingURL=BFormFile.vue_vue_type_script_setup_true_lang-D8IOO-Da.js.map
