'use strict';

var index = require('./index-Ct2DwlDF.js');

const inputCss = () => `:host{font-family:var(--ai12z-font-family) !important;font-size:var(--ai12z-font-size) !important}.input-form{display:flex;flex-direction:column;width:100%;margin-bottom:1rem}.input-container{margin-right:1rem;position:relative;border-radius:4rem;border-width:1px;align-items:stretch;display:flex;}.inner-container{flex-grow:1;padding:0.75rem}.custom-input{color:black;border-width:0;max-height:300px;resize:none;overflow:hidden;background-color:inherit}.custom-input::-moz-placeholder{color:var(--ai12z-input-placeholder-color);opacity:1;font-size:var(--ai12z-input-placeholder-font-size)}.custom-input::placeholder{color:var(--ai12z-input-placeholder-color);opacity:1;font-size:var(--ai12z-input-placeholder-font-size)}.custom-input:focus{outline:none;box-shadow:var(--tw-ring-inset) 0 0 0 calc(0px + var(--tw-ring-offset-width)) var(--ai12z-border-color)}.input-focus-color{border-color:rgb(59 130 246) !important}.plus-icon{color:var(--ai12z-plus-icon-color)}.ai-button{border-left-width:1px;padding-left:1rem;padding-right:1rem;white-space:nowrap;display:inline-flex;place-items:center;color:var(--ai12z-button-font-color);font-size:var(--ai12z-button-font-size);}.ai-button:hover{background-color:var(--ai12z-primary-color);color:rgb(255 255 255)}.custom-svg-checkbox{margin-top:0.25rem;width:1rem;height:1rem;position:absolute}.hide-svg{display:none}.icon-width{width:1.75rem}.custom-attachment{border-right-width:1px;padding-top:10px;padding-left:1rem;padding-right:2rem;cursor:pointer;width:5%}#fileInput{display:none}.error-msg{color:red;font-size:0.75rem;margin-top:0.25rem;text-align:center}.image-Container{position:relative;display:inline-block;margin-right:10px}.delete-image{opacity:3;position:absolute;right:3px;top:0;cursor:pointer;line-height:10px}.btn-bg-color{background-color:var(--ai12z-button-bgcolor)}.searchbar-bgcolor{background-color:var(--ai12z-searchbar-bgcolor)}.inline-searchbar-bgcolor{border-color:var(--ai12z-inlinesearchbar-border-color);background-color:var(--ai12z-inlinesearchbar-bgcolor);border-width:1px}.enter-tip{font-size:12px;color:#666;margin-left:10px}#message-error{margin-left:10px}`;

const ImageUploader = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.imagesChange = index.createEvent(this, "imagesChange", 7);
        this.imageUploadError = index.createEvent(this, "imageUploadError", 7);
        this.capturedImgUpload = index.createEvent(this, "capturedImgUpload", 7);
        this.isElementAvailable = index.createEvent(this, "isElementAvailable", 7);
        this.maxImages = 10;
        this.disabled = false;
        this.images = [];
        this.isEventListenrAttached = false;
        this.handlePaste = (e) => {
            e.stopPropagation();
            e.preventDefault();
            if (e.clipboardData && e.clipboardData.getData("text")) {
                // Ignore text pastes for image uploader
                return;
            }
            const items = (e.clipboardData || window["clipboardData"]).files;
            this.uploadImage(items);
        };
        this.handleDragEnter = (e) => {
            e.preventDefault();
            if (e.target && e.target.style) {
                e.target.style.border = "1px dashed #000";
            }
        };
        this.handleDragOver = (e) => {
            e.preventDefault();
            if (e.target && e.target.style) {
                e.target.style.border = "1px dashed #000";
            }
        };
        this.handleDrop = (e) => {
            e.preventDefault();
            if (e.target && e.target.style) {
                e.target.style.border = "none";
            }
            this.uploadImage(e.dataTransfer.files);
        };
        this.onInputChange = (files) => {
            if (files.length > 0) {
                this.uploadImage(files);
            }
            else {
                this.imageUploadError.emit("No image uploaded.");
                return false;
            }
        };
    }
    handleIsOpened() {
        this.setupEventListeners();
    }
    // @Method()
    setupEventListeners() {
        const container = this.targetElement;
        if (!container)
            return;
        if (this.isEventListenrAttached)
            return;
        this.isEventListenrAttached = true;
        container.addEventListener("paste", this.handlePaste);
        container.addEventListener("dragenter", this.handleDragEnter);
        container.addEventListener("dragover", this.handleDragOver);
        container.addEventListener("drop", this.handleDrop);
    }
    disconnectedCallback() {
        const container = this.el.querySelector(".custom-input");
        if (!container)
            return;
        container.removeEventListener("paste", this.handlePaste);
        container.removeEventListener("dragenter", this.handleDragEnter);
        container.removeEventListener("dragover", this.handleDragOver);
        container.removeEventListener("drop", this.handleDrop);
    }
    handleCapturedImage(event) {
        this.uploadImage(event.detail);
    }
    uploadImage(files) {
        const currentCount = this.images.length;
        const filesArr = Array.from(files);
        // Validate each file
        for (const file of filesArr) {
            // Check file size (5MB limit)
            if (file.size > 5 * 1024 * 1024) {
                this.imageUploadError.emit(`Image "${file.name}" must be under 5MB`);
                return;
            }
            // Check file type
            if (!file.type.startsWith('image/')) {
                this.imageUploadError.emit(`File "${file.name}" is not an image. Only image files are allowed.`);
                return;
            }
        }
        const validFiles = filesArr.filter(file => file.type.match("image.*"));
        if (currentCount + validFiles.length > this.maxImages) {
            this.imageUploadError.emit(`You can upload a maximum of ${this.maxImages} images at a time.`);
            return;
        }
        const readers = validFiles.map(file => {
            return new Promise((resolve, reject) => {
                const reader = new FileReader();
                reader.onload = () => {
                    const mimeType = file.type;
                    const base64Image = `data:${mimeType};base64,${reader.result.split(",")[1]}`;
                    resolve(base64Image);
                };
                reader.onerror = err => {
                    this.imageUploadError.emit("Something went wrong while reading the file.");
                    reject(err);
                };
                reader.readAsDataURL(file);
            });
        });
        Promise.all(readers).then(newImagesArr => {
            const newImages = [...this.images, ...newImagesArr];
            this.imagesChange.emit(newImages);
        });
    }
    async removeImageAtIndex(idx) {
        const newImages = this.images.filter((_, i) => i !== idx);
        this.imagesChange.emit(newImages);
    }
    render() {
        return (index.h("div", { key: 'c9818407a99ed0d89a24d8c561497dd22c451d71', class: "image-uploader" }, index.h("button", { key: 'e4346c7faeb97827b4f0d2140c2aadad17c595f6', type: "button", class: "py-1", disabled: this.disabled, onClick: () => this.uploadElem && this.uploadElem.click(), "aria-label": "Upload Images" }, index.h("div", { key: '3f84c3eb9b71f795c775e96c6bf758c736e0f872', class: "flex items-center gap-2" }, index.h("div", { key: 'fd837005d49b988a562d6b2072ff7fd709621d24' }, index.h("svg", { key: 'b787a29fe238db1c981e8f6afc148b54fe83a8f2', width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg", class: "h-5 w-5 shrink-0" }, index.h("path", { key: '7bba989aa3dfbcca5be561c1931f04866e4fc4bc', "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M18.0322 5.02393C17.7488 5.00078 17.3766 5 16.8 5H11.5002C11.3 6 11.0989 6.91141 10.8903 7.85409C10.7588 8.44955 10.6432 8.97304 10.3675 9.41399C10.1262 9.80009 9.80009 10.1262 9.41399 10.3675C8.97304 10.6432 8.44955 10.7588 7.85409 10.8903C7.81276 10.8994 7.77108 10.9086 7.72906 10.9179L5.21693 11.4762C5.1442 11.4924 5.07155 11.5001 5 11.5002V16.8C5 17.3766 5.00078 17.7488 5.02393 18.0322C5.04612 18.3038 5.0838 18.4045 5.109 18.454C5.20487 18.6422 5.35785 18.7951 5.54601 18.891C5.59546 18.9162 5.69617 18.9539 5.96784 18.9761C6.25118 18.9992 6.62345 19 7.2 19H10C10.5523 19 11 19.4477 11 20C11 20.5523 10.5523 21 10 21H7.16144C6.6343 21 6.17954 21 5.80497 20.9694C5.40963 20.9371 5.01641 20.8658 4.63803 20.673C4.07354 20.3854 3.6146 19.9265 3.32698 19.362C3.13419 18.9836 3.06287 18.5904 3.03057 18.195C2.99997 17.8205 2.99998 17.3657 3 16.8385L3 11C3 8.92477 4.02755 6.93324 5.4804 5.4804C6.93324 4.02755 8.92477 3 11 3L16.8385 3C17.3657 2.99998 17.8205 2.99997 18.195 3.03057C18.5904 3.06287 18.9836 3.13419 19.362 3.32698C19.9265 3.6146 20.3854 4.07354 20.673 4.63803C20.8658 5.01641 20.9371 5.40963 20.9694 5.80497C21 6.17954 21 6.6343 21 7.16144V10C21 10.5523 20.5523 11 20 11C19.4477 11 19 10.5523 19 10V7.2C19 6.62345 18.9992 6.25118 18.9761 5.96784C18.9539 5.69617 18.9162 5.59546 18.891 5.54601C18.7951 5.35785 18.6422 5.20487 18.454 5.109C18.4045 5.0838 18.3038 5.04612 18.0322 5.02393ZM5.28014 9.41336L7.2952 8.96556C8.08861 8.78925 8.24308 8.74089 8.35381 8.67166C8.48251 8.59121 8.59121 8.48251 8.67166 8.35381C8.74089 8.24308 8.78925 8.08861 8.96556 7.2952L9.41336 5.28014C8.51014 5.59289 7.63524 6.15398 6.89461 6.89461C6.15398 7.63524 5.59289 8.51014 5.28014 9.41336ZM17 15C17 14.4477 17.4477 14 18 14C18.5523 14 19 14.4477 19 15V17H21C21.5523 17 22 17.4477 22 18C22 18.5523 21.5523 19 21 19H19V21C19 21.5523 18.5523 22 18 22C17.4477 22 17 21.5523 17 21V19H15C14.4477 19 14 18.5523 14 18C14 17.4477 14.4477 17 15 17H17V15Z", fill: "currentColor" }))), index.h("div", { key: '4c3feb42c66cc98af2b8cc5a863643a671f9cf00' }, "Upload Images"))), index.h("input", { key: 'fe0ae0769c82a7c76f1bb9b9ba97de26cc2f765c', type: "file", multiple: true, accept: "image/*", style: { display: "none" }, disabled: this.disabled, onChange: ($event) => this.onInputChange($event.target.files), ref: el => (this.uploadElem = el) })));
    }
    get el() { return index.getElement(this); }
};
ImageUploader.style = inputCss();

const keyboardShortcutsDialogCss = () => `:host{font-family:var(--ai12z-bot-font-family, ui-sans-serif, system-ui, sans-serif)}.shortcuts-backdrop{position:fixed;top:0;left:0;width:100%;height:100%;background-color:rgba(0, 0, 0, 0.6);display:flex;justify-content:center;align-items:center;isolation:isolate;z-index:20000;animation:fadeIn 0.2s ease-out}@keyframes fadeIn{from{opacity:0}to{opacity:1}}.shortcuts-content{background:white;border-radius:12px;box-shadow:0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);max-width:600px;width:90%;max-height:80vh;display:flex;flex-direction:column;animation:slideUp 0.3s ease-out}@keyframes slideUp{from{opacity:0;transform:translateY(20px)}to{opacity:1;transform:translateY(0)}}.shortcuts-header{display:flex;justify-content:space-between;align-items:center;padding:24px 24px 16px;border-bottom:1px solid #e5e7eb}.shortcuts-header h2{margin:0;font-size:20px;font-weight:600;color:#111827}.close-button{background:none;border:none;padding:8px;cursor:pointer;color:#636b74;border-radius:6px;display:flex;align-items:center;justify-content:center;transition:all 0.2s ease}.close-button:hover{background-color:#f3f4f6;color:#111827}.close-button:focus{outline:2px solid #3b82f6;outline-offset:2px}.shortcuts-body{overflow-y:auto;padding:20px 24px;flex:1}.shortcuts-category{margin-bottom:24px}.shortcuts-category:last-child{margin-bottom:0}.shortcuts-category h3{font-size:14px;font-weight:600;color:#6b7280;text-transform:uppercase;letter-spacing:0.05em;margin:0 0 12px 0}.shortcuts-list{display:flex;flex-direction:column;gap:8px}.shortcut-item{display:flex;justify-content:space-between;align-items:center;padding:10px 12px;background-color:#f9fafb;border-radius:6px;transition:background-color 0.2s ease}.shortcut-item:hover{background-color:#f3f4f6}.shortcut-description{font-size:14px;color:#374151;flex:1}.shortcut-keys{display:flex;align-items:center;gap:4px}.key{display:inline-flex;align-items:center;justify-content:center;min-width:28px;height:28px;padding:0 8px;font-size:13px;font-weight:500;font-family:ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;color:#374151;background-color:white;border:1px solid #d1d5db;border-radius:4px;box-shadow:0 1px 2px 0 rgba(0, 0, 0, 0.05)}.key-separator{font-size:12px;color:#9ca3af;margin:0 2px}.shortcuts-footer{padding:16px 24px;border-top:1px solid #e5e7eb;background-color:#f9fafb;border-radius:0 0 12px 12px}.shortcuts-hint{margin:0;font-size:13px;color:#6b7280;text-align:center}.key-inline{display:inline-flex;align-items:center;justify-content:center;min-width:24px;height:24px;padding:0 6px;font-size:12px;font-weight:500;font-family:ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;color:#374151;background-color:white;border:1px solid #d1d5db;border-radius:4px;margin:0 2px}.shortcuts-body::-webkit-scrollbar{width:8px}.shortcuts-body::-webkit-scrollbar-track{background:#f1f1f1;border-radius:4px}.shortcuts-body::-webkit-scrollbar-thumb{background:#d1d5db;border-radius:4px}.shortcuts-body::-webkit-scrollbar-thumb:hover{background:#9ca3af}@media (max-width: 640px){.shortcuts-content{width:95%;max-height:90vh}.shortcuts-header{padding:20px 16px 12px}.shortcuts-header h2{font-size:18px}.shortcuts-body{padding:16px}.shortcut-item{flex-direction:column;align-items:flex-start;gap:8px}.shortcuts-footer{padding:12px 16px}}`;

const KeyboardShortcutsDialog = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.closeDialog = index.createEvent(this, "closeDialog", 7);
        this.show = false;
        this.shortcuts = [];
        this.groupedShortcuts = [];
        this.isMac = typeof navigator !== "undefined" && navigator.platform.toUpperCase().indexOf("MAC") >= 0;
        this.KEY_MAP = {
            Meta: this.isMac ? "⌘" : "Win",
            Control: this.isMac ? "⌃" : "Ctrl",
            Alt: this.isMac ? "⌥" : "Alt",
            Shift: this.isMac ? "⇧" : "Shift",
            Enter: "↵",
            Escape: "Esc",
            ArrowUp: "↑",
            ArrowDown: "↓",
            ArrowLeft: "←",
            ArrowRight: "→",
        };
        this.handleKeyDown = (event) => {
            if (event.key === "Escape") {
                this.handleClose();
            }
            if (event.key !== "Tab")
                return;
            const activeElement = this.el.shadowRoot.activeElement;
            if (event.shiftKey) {
                if (activeElement === this.firstFocusable) {
                    event.preventDefault();
                    this.lastFocusable.focus();
                }
            }
            else {
                if (activeElement === this.lastFocusable) {
                    event.preventDefault();
                    this.firstFocusable.focus();
                }
            }
        };
    }
    handleShowChange(newValue) {
        if (newValue) {
            setTimeout(() => {
                this.initFocusTrap();
            });
        }
    }
    handleShortcutsChange() {
        this.groupedShortcuts = this.getGroupedShortcuts();
    }
    componentWillLoad() {
        this.groupedShortcuts = this.getGroupedShortcuts();
    }
    initFocusTrap() {
        const dialog = this.el.shadowRoot.querySelector(".shortcuts-content");
        const focusable = dialog.querySelectorAll(`
      button,
      [href],
      input,
      select,
      textarea,
      [tabindex]:not([tabindex="-1"])
    `);
        if (!focusable.length)
            return;
        this.firstFocusable = focusable[0];
        this.lastFocusable = focusable[focusable.length - 1];
        this.firstFocusable.focus();
    }
    handleClose() {
        this.closeDialog.emit();
    }
    formatKey(key) {
        return this.KEY_MAP[key] || key;
    }
    getGroupedShortcuts() {
        const grouped = this.shortcuts.reduce((acc, shortcut) => {
            if (!acc[shortcut.category]) {
                acc[shortcut.category] = [];
            }
            acc[shortcut.category].push(shortcut);
            return acc;
        }, {});
        return Object.entries(grouped);
    }
    render() {
        if (!this.show) {
            return null;
        }
        return (index.h("div", { role: "dialog", "aria-modal": "true", "aria-labelledby": "shortcuts-dialog-title", class: "shortcuts-backdrop", onClick: () => this.handleClose(), onKeyDown: this.handleKeyDown }, index.h("div", { class: "shortcuts-content", role: "document", tabindex: "-1", onClick: e => e.stopPropagation() }, index.h("div", { class: "shortcuts-header" }, index.h("h2", { id: "shortcuts-dialog-title" }, "Keyboard Shortcuts"), index.h("button", { class: "close-button", onClick: () => this.handleClose(), "aria-label": "Close shortcuts dialog" }, index.h("svg", { width: "20", height: "20", viewBox: "0 0 20 20", fill: "none" }, index.h("path", { d: "M15 5L5 15M5 5L15 15", stroke: "currentColor", "stroke-width": "2", "stroke-linecap": "round" })))), index.h("div", { class: "shortcuts-body" }, this.groupedShortcuts.map(([category, shortcuts]) => (index.h("div", { class: "shortcuts-category" }, index.h("h3", null, category), index.h("div", { class: "shortcuts-list" }, shortcuts.map(shortcut => {
            var _a;
            return (index.h("div", { class: "shortcut-item", tabindex: "0" }, index.h("span", { class: "shortcut-description" }, shortcut.description), index.h("div", { class: "shortcut-keys" }, (_a = shortcut.modifiers) === null || _a === void 0 ? void 0 :
                _a.map((modifier, index$1) => (index.h("span", null, index.h("kbd", { class: "key" }, this.formatKey(modifier)), index$1 < shortcut.modifiers.length - 1 && index.h("span", { class: "key-separator" }, "+")))), shortcut.modifiers && shortcut.modifiers.length > 0 && index.h("span", { class: "key-separator" }, "+"), index.h("kbd", { class: "key" }, this.formatKey(shortcut.key)))));
        })))))), index.h("div", { class: "shortcuts-footer" }, index.h("p", { class: "shortcuts-hint" }, "Press ", index.h("kbd", { class: "key-inline" }, this.formatKey("Escape")), " to close")))));
    }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "show": [{
                "handleShowChange": 0
            }],
        "shortcuts": [{
                "handleShortcutsChange": 0
            }]
    }; }
};
KeyboardShortcutsDialog.style = keyboardShortcutsDialogCss();

exports.image_uploader = ImageUploader;
exports.keyboard_shortcuts_dialog = KeyboardShortcutsDialog;
