'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-9a9efa1c.js');
const index$1 = require('./index-61ff1b55.js');
const minus = require('./minus-5b6b8813.js');
const plus = require('./plus-339686b5.js');
const v4 = require('./v4-9b297151.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 = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.allFormValues = index.createEvent(this, "allFormValues", 7);
        this.label = null;
        this.placeholder = null;
        this.addRowButtonText = null;
        this.rowData = [];
    }
    get hostElement() { return index.getElement(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', index$1.debounce(this.handleInput.bind(this), 500));
        });
    }
    generateUniqueId() {
        return v4.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 (index.h(index.Host, { key: 'c19a4c61c107632a3e97217c18ee4c2076ae077e' }, index.h("div", { key: '71fb803627c006e137db1f6be32fb26ab144c876', ref: el => (this.pnSlotElement = el), class: "multiple-input-slot" }, this.rowData?.map((data, i) => {
            return (index.h("div", { class: "multiple-input-generated-row" }, index.h("pn-marketweb-input", { label: this.label, type: "text", placeholder: this.placeholder, value: data.input, id: data.id }), i > 0 ? (index.h("pn-button", { arialabel: "Delete", icon: minus.minus, appearance: "warning", onClick: () => {
                    this.deleteRow(i);
                }, index: i, id: data.id }, "Delete")) : null));
        })), index.h("dvi", { key: 'dc7c599822a8e8c39064ecc2ff76cea767ef757f', class: "multiple-input-row" }, index.h("pn-button", { key: '9289b646075f389d935f8a8be3f2490d0e102005', tooltip: this.addRowButtonText ? this.addRowButtonText : null, "left-icon": "true", arialabel: "Add", icon: plus.plus, appearance: "light", variant: "borderless", class: "connected-dropdown__button-right", onClick: () => {
                this.addRow();
            } }, this.addRowButtonText))));
    }
};
PnMultipleInput.style = PnMultipleInputStyle0;

exports.pn_multiple_input = PnMultipleInput;

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