import { p as proxyCustomElement, H, c as createEvent, h, F as Fragment, d as Host } from './p-AiOs8RvA.js';
import { i as interact } from './p-CGNX5HqF.js';
import { updateField, VerdocsEndpoint, getRGBA } from '@verdocs/js-sdk';
import { S as SettingsIcon } from './p-7LXbcjVz.js';
import { S as Store } from './p-DoY8Nb0Y.js';
import { d as defineCustomElement$9 } from './p-DMVmShTi.js';
import { d as defineCustomElement$8 } from './p-B4xcxu5g.js';
import { d as defineCustomElement$7 } from './p-Bk16Ytky.js';
import { d as defineCustomElement$6 } from './p-Bbomvxyl.js';
import { d as defineCustomElement$5 } from './p-I7r_G5nS.js';
import { d as defineCustomElement$4 } from './p-BQtWBtuj.js';
import { d as defineCustomElement$3 } from './p-Cs2yaGJp.js';
import { d as defineCustomElement$2 } from './p-Bvhap2eA.js';
import { d as defineCustomElement$1 } from './p-xz67XcVg.js';

const verdocsFieldTextboxCss = "@-webkit-keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}@keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}verdocs-field-textbox{font-family:\"Inter\", -apple-system, \"Segoe UI\", \"Roboto\", \"Helvetica Neue\", sans-serif;-webkit-box-sizing:border-box;box-sizing:border-box;width:150px;height:15px;display:block;font-size:11px;position:relative;scroll-margin:20px 0;letter-spacing:-0.2px;background-color:transparent;-webkit-transform-origin:bottom left;transform-origin:bottom left;border:1px solid rgba(0, 0, 0, 0.2)}verdocs-field-textbox .edge-top,verdocs-field-textbox .edge-right,verdocs-field-textbox .edge-left,verdocs-field-textbox .edge-bottom{position:absolute;z-index:10}verdocs-field-textbox .edge-top{top:-2px;left:0;right:0;height:5px}verdocs-field-textbox .edge-right{top:0;bottom:0;right:-2px;width:5px}verdocs-field-textbox .edge-left{top:0;left:-2px;bottom:0;width:5px}verdocs-field-textbox .edge-bottom{bottom:-2px;left:0;right:0;height:5px}verdocs-field-textbox input{cursor:inherit;width:100%;height:100%;border:none;outline:none;padding:0 3px;font-size:11px;background:none;font-weight:500;position:absolute;-webkit-transform-origin:0 0;transform-origin:0 0;-webkit-box-sizing:border-box;box-sizing:border-box;color:rgba(0, 0, 0, 0.87)}verdocs-field-textbox input.hide{display:none}verdocs-field-textbox.disabled input{opacity:0.5}verdocs-field-textbox textarea{cursor:inherit;width:100%;height:100%;resize:none;border:none;outline:none;padding:0 3px;font-size:11px;background:none;font-weight:500;-webkit-transform-origin:0 0;transform-origin:0 0;-webkit-box-sizing:border-box;box-sizing:border-box;color:rgba(0, 0, 0, 0.87)}verdocs-field-textbox textarea.hide{display:none}verdocs-field-textbox.disabled textarea{opacity:0.5}verdocs-field-textbox label{top:-14px;left:-1px;height:14px;color:white;padding:0 4px;font-size:9px;line-height:13px;position:absolute;background:#4a4a99;border-top-left-radius:2px;border-top-right-radius:2px}verdocs-field-textbox.required{border:1px solid rgb(204, 0, 0)}verdocs-field-textbox.focused{-webkit-animation:verdocs-field-pulse 0.4s 1;animation:verdocs-field-pulse 0.4s 1}verdocs-field-textbox verdocs-button-panel{top:-3px;left:-2px;opacity:0.5;z-index:1000;cursor:pointer;position:absolute;-webkit-transform:scale(0.6);transform:scale(0.6)}verdocs-field-textbox verdocs-button-panel[data-active],verdocs-field-textbox verdocs-button-panel:hover{opacity:1}verdocs-field-textbox verdocs-button-panel .icon svg{fill:#333333}verdocs-field-textbox verdocs-button-panel .icon:hover svg{fill:#000000}verdocs-field-textbox [data-lastpass-icon-root]{display:none !important}verdocs-field-textbox .settings-icon{position:absolute;top:-4px;left:-18px;display:inline-block;cursor:pointer;opacity:0.3}verdocs-field-textbox .settings-icon svg{fill:#707ae5}verdocs-field-textbox .settings-icon:hover{opacity:1}@keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}";

const VerdocsFieldTextbox = /*@__PURE__*/ proxyCustomElement(class VerdocsFieldTextbox extends H {
    constructor() {
        super();
        this.__registerHost();
        this.settingsChanged = createEvent(this, "settingsChanged");
        this.deleted = createEvent(this, "deleted");
        /**
         * Fields may be attached to templates or envelopes, but only template fields may be edited.
         */
        this.source = 'template';
        /**
         * The source template or envelope ID the field is found in.
         */
        this.sourceid = '';
        /**
         * The name of the field to display.
         */
        this.fieldname = '';
        /**
         * Override the field's settings. This is intended to be used during signing when fields are being
         * mutated.
         */
        this.field = null;
        /**
         * If set, overrides the field's settings object. Primarily used to support "preview" modes where all fields are disabled.
         */
        this.disabled = false;
        /**
         * If set, overrides the field's settings object. Primarily used to support "preview" modes where all fields are disabled.
         */
        this.multiline = false;
        /**
         * If set, a settings icon will be displayed on hover. The settings shown allow the field's recipient and other settings to be
         * changed, so it should typically only be enabled in the Builder.
         */
        this.editable = false;
        /**
         * If set, the field may be dragged to a new location. This should only be enabled in the Builder, or for self-placed fields.
         */
        this.moveable = false;
        /**
         * If set, the field is considered "done" and is drawn in a display-final-value state.
         */
        this.done = false;
        /**
         * If set, the field will be be scaled horizontally by this factor.
         */
        this.xscale = 1;
        /**
         * If set, the field will be be scaled vertically by this factor.
         */
        this.yscale = 1;
        /**
         * The page the field is on
         */
        this.pagenumber = 1;
        this.showingProperties = false;
        this.focused = false;
    }
    async focusField() {
        this.inputEl.focus();
    }
    async showSettingsPanel() {
        const settingsPanel = document.getElementById(`verdocs-settings-panel-${this.fieldname}`);
        if (settingsPanel && settingsPanel.showPanel) {
            settingsPanel.showPanel();
        }
    }
    async hideSettingsPanel() {
        const settingsPanel = document.getElementById(`verdocs-settings-panel-${this.fieldname}`);
        if (settingsPanel && settingsPanel.hidePanel) {
            settingsPanel.hidePanel();
        }
        this.showingProperties = false;
    }
    componentDidRender() {
        interact.dynamicDrop(true);
        if (this.editable) {
            interact(this.el).resizable({
                edges: {
                    top: '.edge-top',
                    left: '.edge-left',
                    bottom: '.edge-bottom',
                    right: '.edge-right',
                },
                modifiers: [
                    interact.modifiers.restrictSize({
                        min: { width: 30, height: 15 },
                    }),
                ],
                listeners: {
                    start: this.handleResizeStart.bind(this),
                    move: this.handleResize.bind(this),
                    end: this.handleResizeEnd.bind(this),
                },
            });
        }
    }
    handleResizeStart(e) {
        e.preventDefault();
        e.stopPropagation();
    }
    handleResize(e) {
        let { width, height } = e.rect;
        const dX = e.deltaRect.left;
        const dY = e.deltaRect.bottom;
        const currentLeft = parseFloat(e.target.style.left);
        const currentBottom = parseFloat(e.target.style.bottom);
        width /= this.xscale;
        height /= this.yscale;
        Object.assign(e.target.style, {
            width: `${width}px`,
            height: `${height}px`,
            left: `${currentLeft + dX}px`,
            bottom: `${currentBottom - dY}px`,
        });
    }
    handleResizeEnd(e) {
        const { sourceid, fieldname } = this;
        const width = Math.round(parseFloat(e.target.style.width));
        let height = Math.round(parseFloat(e.target.style.height));
        if (height < 20) {
            height = 15;
        }
        const multiline = height > 15;
        updateField(VerdocsEndpoint.getDefault(), sourceid, fieldname, { width, height, multiline })
            .then(async (updatedField) => {
            var _a;
            const template = await Store.getTemplate(VerdocsEndpoint.getDefault(), this.sourceid);
            const newTemplate = JSON.parse(JSON.stringify(template));
            const fieldIndex = newTemplate.fields.findIndex(field => field.name === fieldname);
            if (fieldIndex > -1) {
                newTemplate.fields[fieldIndex] = updatedField;
            }
            Store.updateTemplate(this.sourceid, newTemplate);
            (_a = this.settingsChanged) === null || _a === void 0 ? void 0 : _a.emit({ fieldName: fieldname, field: updatedField });
            Object.assign(e.target.dataset, { x: 0, y: 0, h: 0 });
        })
            .catch(e => console.log('Field update failed', e));
    }
    render() {
        const { source, sourceid, fieldname, editable = false, done = false, disabled = false, focused, xscale = 1, yscale = 1 } = this;
        const { index, field } = Store.getField(source, sourceid, fieldname, this.field);
        let { required = false, placeholder = '', label = '', width = 150, default: value = '', multiline = false } = field || {};
        const backgroundColor = getRGBA(index);
        // TODO: Consolidate value/defaultValue handling between template and envelope fields.
        if (field === null || field === void 0 ? void 0 : field.value) {
            value = field === null || field === void 0 ? void 0 : field.value;
        }
        // TODO: This is an outdated technique from the old system. We should compute it.
        const maxlength = width / 5;
        if (done) {
            return h(Host, { class: { done } }, value);
        }
        return (h(Host, { class: { required, disabled, done, focused }, style: { backgroundColor } }, editable && h("div", { class: "edge-top" }), editable && h("div", { class: "edge-right" }), editable && h("div", { class: "edge-left" }), editable && h("div", { class: "edge-bottom" }), label && h("label", null, label), multiline ? (h("textarea", { name: fieldname, disabled: disabled, required: required, placeholder: placeholder, ref: el => (this.inputEl = el), onFocus: () => (this.focused = true), onBlur: () => (this.focused = false) }, value)) : (h("input", { type: "text", name: fieldname, value: value, disabled: disabled, required: required, placeholder: placeholder, maxlength: maxlength, ref: el => (this.inputEl = el), onFocus: () => (this.focused = true), onBlur: () => (this.focused = false) })), editable && (h(Fragment, null, h("div", { id: `verdocs-settings-panel-trigger-${fieldname}`, style: { transform: `scale(${Math.floor((1 / xscale) * 1000) / 1000}, ${Math.floor((1 / yscale) * 1000) / 1000})` }, class: "settings-icon", innerHTML: SettingsIcon, onClick: (e) => {
                e.stopPropagation();
                this.showingProperties = !this.showingProperties;
            } }), this.showingProperties && (h("verdocs-portal", { anchor: `verdocs-settings-panel-trigger-${fieldname}`, onClickAway: () => (this.showingProperties = false) }, h("verdocs-template-field-properties", { templateId: sourceid, fieldName: fieldname, onClose: () => this.hideSettingsPanel(), onDelete: () => {
                var _a;
                (_a = this.deleted) === null || _a === void 0 ? void 0 : _a.emit({ fieldName: fieldname });
                return this.hideSettingsPanel();
            }, onSettingsChanged: e => {
                var _a;
                (_a = this.settingsChanged) === null || _a === void 0 ? void 0 : _a.emit(e.detail);
                return this.hideSettingsPanel();
            }, helpText: 'Text boxes may be used to capture simple text input. Participant-entered values will be stored for later retrieval via the "name" field.<br /><br />If marked required, the participant must complete the field before proceeding.' })))))));
    }
    get el() { return this; }
    static get style() { return verdocsFieldTextboxCss; }
}, [0, "verdocs-field-textbox", {
        "source": [513],
        "sourceid": [513],
        "fieldname": [513],
        "field": [16],
        "disabled": [516],
        "multiline": [516],
        "editable": [516],
        "moveable": [516],
        "done": [516],
        "xscale": [514],
        "yscale": [514],
        "pagenumber": [514],
        "showingProperties": [32],
        "focused": [32],
        "focusField": [64],
        "showSettingsPanel": [64],
        "hideSettingsPanel": [64]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["verdocs-field-textbox", "verdocs-button", "verdocs-checkbox", "verdocs-component-error", "verdocs-help-icon", "verdocs-loader", "verdocs-portal", "verdocs-select-input", "verdocs-template-field-properties", "verdocs-text-input"];
    components.forEach(tagName => { switch (tagName) {
        case "verdocs-field-textbox":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, VerdocsFieldTextbox);
            }
            break;
        case "verdocs-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "verdocs-checkbox":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "verdocs-component-error":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "verdocs-help-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "verdocs-loader":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "verdocs-portal":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "verdocs-select-input":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "verdocs-template-field-properties":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "verdocs-text-input":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}

export { VerdocsFieldTextbox as V, defineCustomElement as d };
//# sourceMappingURL=p-01f6WUnw.js.map

//# sourceMappingURL=p-01f6WUnw.js.map