import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
import { d as debounce } from './index2.js';
import { m as minus } from './minus.js';
import { p as plus } from './plus.js';
import { d as defineCustomElement$2 } from './pn-marketweb-input2.js';
import { v as v4 } from './v4.js';

const pnMultipleInputCss = "pn-multiple-input .multiple-input-generated-row,pn-multiple-input .multiple-input-row{display:flex;gap:1rem}pn-multiple-input .multiple-input-generated-row pn-marketweb-input,pn-multiple-input .multiple-input-row pn-marketweb-input{width:100%}pn-multiple-input .multiple-input-slot{display:flex;gap:1rem;flex-direction:column;margin-top:1rem;margin-bottom:1rem}";
const PnMultipleInputStyle0 = pnMultipleInputCss;

const PnMultipleInput$1 = /*@__PURE__*/ proxyCustomElement(class PnMultipleInput extends HTMLElement {
    constructor() {
        super();
        this.__registerHost();
        this.allFormValues = createEvent(this, "allFormValues", 7);
        this.label = null;
        this.placeholder = null;
        this.addRowButtonText = null;
        this.rowData = [];
    }
    get hostElement() { return this; }
    pnSlotElement = null;
    _formValues = [];
    allFormValues;
    componentWillLoad() {
        const elemtnId = this.generateUniqueId();
        const newRow = { id: elemtnId, input: '' };
        this.rowData = [...this.rowData, newRow];
    }
    componentDidLoad() {
        this.addEventHandlers();
    }
    componentDidUpdate() {
        this.addEventHandlers();
    }
    addEventHandlers() {
        const rows = this.pnSlotElement.querySelectorAll('pn-marketweb-input');
        rows.forEach(element => {
            element.addEventListener('keyup', debounce(this.handleInput.bind(this), 500));
        });
    }
    generateUniqueId() {
        return v4();
    }
    deleteRow(indexToRemove) {
        if (indexToRemove < 0) {
            return;
        }
        this.rowData.splice(indexToRemove, 1);
        this.rowData = [...this.rowData];
        this.emitValues();
    }
    addRow() {
        const newRow = { id: this.generateUniqueId(), input: '' };
        this.rowData = [...this.rowData, newRow];
        this.emitValues();
    }
    handleInput(e) {
        const target = e.target;
        const inputElm = target.closest('pn-marketweb-input');
        if (!inputElm) {
            return;
        }
        const inputElmId = inputElm.id;
        const value = target.value;
        const index = this.rowData.findIndex(item => item.id === inputElmId);
        if (index < 0) {
            return;
        }
        this.rowData[index] = {
            ...this.rowData[index],
            input: value,
        };
        this.emitValues();
    }
    emitValues() {
        this._formValues = [];
        const label = this.label ? this.label : 'InputField';
        this.rowData.forEach(item => {
            const rowValue = { row: [] };
            if (item.input) {
                const dropDownValue = { name: label, value: item.input };
                rowValue.row.push(dropDownValue);
            }
            if (rowValue.row.length === 1) {
                this._formValues.push(rowValue);
            }
        });
        if (this._formValues.length === this.rowData.length) {
            const valueJsonString = JSON.stringify(this._formValues);
            this.allFormValues.emit(valueJsonString);
        }
        else {
            this.allFormValues.emit('');
        }
    }
    render() {
        return (h(Host, { key: 'c19a4c61c107632a3e97217c18ee4c2076ae077e' }, h("div", { key: '71fb803627c006e137db1f6be32fb26ab144c876', ref: el => (this.pnSlotElement = el), class: "multiple-input-slot" }, this.rowData?.map((data, i) => {
            return (h("div", { class: "multiple-input-generated-row" }, h("pn-marketweb-input", { label: this.label, type: "text", placeholder: this.placeholder, value: data.input, id: data.id }), i > 0 ? (h("pn-button", { arialabel: "Delete", icon: minus, appearance: "warning", onClick: () => {
                    this.deleteRow(i);
                }, index: i, id: data.id }, "Delete")) : null));
        })), h("dvi", { key: 'dc7c599822a8e8c39064ecc2ff76cea767ef757f', class: "multiple-input-row" }, h("pn-button", { key: '9289b646075f389d935f8a8be3f2490d0e102005', tooltip: this.addRowButtonText ? this.addRowButtonText : null, "left-icon": "true", arialabel: "Add", icon: plus, appearance: "light", variant: "borderless", class: "connected-dropdown__button-right", onClick: () => {
                this.addRow();
            } }, this.addRowButtonText))));
    }
    static get style() { return PnMultipleInputStyle0; }
}, [0, "pn-multiple-input", {
        "label": [1],
        "placeholder": [1],
        "addRowButtonText": [1, "add-row-button-text"],
        "rowData": [32]
    }]);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["pn-multiple-input", "pn-marketweb-input"];
    components.forEach(tagName => { switch (tagName) {
        case "pn-multiple-input":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, PnMultipleInput$1);
            }
            break;
        case "pn-marketweb-input":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}

const PnMultipleInput = PnMultipleInput$1;
const defineCustomElement = defineCustomElement$1;

export { PnMultipleInput, defineCustomElement };

//# sourceMappingURL=pn-multiple-input.js.map