"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __rest = (this && this.__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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileUploadDropzone = void 0;
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
const react_1 = __importStar(require("react"));
const aksel_icons_1 = require("@navikt/aksel-icons");
const button_1 = require("../../../button");
const typography_1 = require("../../../typography");
const utils_external_1 = require("../../../utils-external");
const helpers_1 = require("../../../utils/helpers");
const i18n_hooks_1 = require("../../../utils/i18n/i18n.hooks");
const useFormField_1 = require("../../useFormField");
const useFileUpload_1 = require("../hooks/useFileUpload");
const FileUploadRoot_context_1 = require("../root/FileUploadRoot.context");
const useDropzone_1 = require("./useDropzone");
const FileUploadDropzone = (0, react_1.forwardRef)((props, ref) => {
    const { onSelect, error, label, description, className, multiple = true, accept, validator, maxSizeInBytes, fileLimit, icon: DropzoneIcon = aksel_icons_1.CloudUpIcon, disabled, translations, onClick, id: buttonId, "data-color": color } = props, rest = __rest(props, ["onSelect", "error", "label", "description", "className", "multiple", "accept", "validator", "maxSizeInBytes", "fileLimit", "icon", "disabled", "translations", "onClick", "id", "data-color"]);
    const context = (0, FileUploadRoot_context_1.useFileUploadTranslation)(false);
    const translate = (0, i18n_hooks_1.useI18n)("FileUpload", { dropzone: translations }, context === null || context === void 0 ? void 0 : context.translations);
    const fileLimitReached = fileLimit && (fileLimit === null || fileLimit === void 0 ? void 0 : fileLimit.current) >= (fileLimit === null || fileLimit === void 0 ? void 0 : fileLimit.max) && (fileLimit === null || fileLimit === void 0 ? void 0 : fileLimit.max) > 0;
    const _disabled = disabled !== null && disabled !== void 0 ? disabled : fileLimitReached;
    const { inputProps, errorId, showErrorMsg, hasError, inputDescriptionId } = (0, useFormField_1.useFormField)(Object.assign(Object.assign({}, (0, utils_external_1.omit)(props, ["id"])), { disabled: _disabled }), "fileUpload");
    const { id: inputId, "aria-describedby": ariaDescribedby } = inputProps, inputPropsRest = __rest(inputProps, ["id", "aria-describedby"]);
    const labelId = (0, utils_external_1.useId)();
    const { upload, onChange, inputRef, mergedRef } = (0, useFileUpload_1.useFileUpload)({
        ref,
        onSelect,
        validator,
        accept,
        maxSizeInBytes,
        disabled: inputProps.disabled,
    });
    const dropzoneCtx = (0, useDropzone_1.useDropzone)({
        upload,
        disabled: inputProps.disabled,
    });
    return (react_1.default.createElement("div", { className: (0, helpers_1.cl)("aksel-form-field", "aksel-dropzone", className, {
            "aksel-dropzone--error": hasError,
            "aksel-dropzone--dragging": dropzoneCtx.isDraggingOver,
            "aksel-dropzone--disabled": inputProps.disabled,
        }) },
        react_1.default.createElement(typography_1.Label, { htmlFor: inputId, id: labelId, className: "aksel-form-field__label" }, label),
        !!description && (react_1.default.createElement(typography_1.BodyShort, { id: inputDescriptionId, className: "aksel-form-field__description", as: "div" }, description)),
        react_1.default.createElement("div", { className: "aksel-dropzone__area", onDragEnter: dropzoneCtx.onDragEnter, onDragOver: dropzoneCtx.onDragOver, onDragLeave: dropzoneCtx.onDragLeave, onDrop: dropzoneCtx.onDrop, onClick: (0, helpers_1.composeEventHandlers)(onClick, () => { var _a; return (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }), "data-disabled": inputProps.disabled, "data-color": color },
            !inputProps.disabled && (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("div", { className: "aksel-dropzone__area-icon" },
                    react_1.default.createElement(DropzoneIcon, { fontSize: "1.5rem", "aria-hidden": true })),
                react_1.default.createElement("div", { className: "aksel-dropzone__area-release" },
                    react_1.default.createElement("div", { className: "aksel-dropzone__area-release__icon" },
                        react_1.default.createElement(DropzoneIcon, { "aria-hidden": true })),
                    react_1.default.createElement("span", { "aria-hidden": !dropzoneCtx.isDraggingOver, className: "aksel-dropzone__area-release__text" }, translate("dropzone.drop"))),
                react_1.default.createElement("div", { "aria-hidden": true },
                    react_1.default.createElement(typography_1.BodyShort, { as: "div", spacing: true }, multiple
                        ? translate("dropzone.dragAndDropMultiple")
                        : translate("dropzone.dragAndDrop")),
                    react_1.default.createElement(typography_1.BodyShort, { as: "div" }, translate("dropzone.or"))),
                react_1.default.createElement(button_1.Button, Object.assign({}, (0, utils_external_1.omit)(rest, ["errorId"]), inputPropsRest, { id: buttonId, "aria-describedby": (0, helpers_1.cl)(labelId, ariaDescribedby), className: "aksel-dropzone__area-button", type: "button", variant: "secondary" }), multiple
                    ? translate("dropzone.buttonMultiple")
                    : translate("dropzone.button")))),
            inputProps.disabled && (react_1.default.createElement("div", { className: "aksel-dropzone__area-disabled" },
                react_1.default.createElement(aksel_icons_1.CircleSlashIcon, { "aria-hidden": true, fontSize: "1.75rem" }),
                react_1.default.createElement(typography_1.BodyShort, { as: "div" }, fileLimitReached
                    ? translate("dropzone.disabledFilelimit")
                    : translate("dropzone.disabled")))),
            react_1.default.createElement("input", { id: inputId, type: "file", style: { display: "none" }, multiple: multiple, accept: accept, onChange: onChange, ref: mergedRef, disabled: inputProps.disabled })),
        react_1.default.createElement("div", { className: "aksel-form-field__error", id: errorId, "aria-relevant": "additions removals", "aria-live": "polite" }, showErrorMsg && react_1.default.createElement(typography_1.ErrorMessage, { showIcon: true }, error))));
});
exports.FileUploadDropzone = FileUploadDropzone;
exports.default = FileUploadDropzone;
//# sourceMappingURL=FileUploadDropzone.js.map