import { h } from "@stencil/core";
import getAssetPath from "../../utils/assetPath";
export class EclFileUpload {
    constructor() {
        this.theme = 'ec';
        this.styleClass = undefined;
        this.inputClass = undefined;
        this.eclScript = false;
        this.disabled = false;
        this.required = false;
        this.invalid = false;
        this.helperId = undefined;
        this.helperText = undefined;
        this.placeholder = undefined;
        this.width = 'm';
        this.label = undefined;
        this.type = undefined;
        this.inputId = undefined;
        this.name = undefined;
        this.multiple = false;
        this.defaultValue = undefined;
        this.buttonChooseLabel = undefined;
        this.buttonReplaceLabel = undefined;
    }
    getClass() {
        const styleClasses = ['ecl-input', this.styleClass];
        return styleClasses.join(' ');
    }
    getInputClasses() {
        const inputClasses = ['ecl-file-upload'];
        if (this.inputClass) {
            inputClasses.push(this.inputClass);
        }
        return inputClasses.join(' ');
    }
    componentDidRender() {
        if (this.inputId) {
            const group = this.el.closest('.ecl-form-group');
            if (group) {
                const label = group.querySelector('.ecl-form-label');
                if (label) {
                    label.setAttribute('for', this.inputId);
                    label.setAttribute('id', `${this.inputId}-label`);
                }
                const helper = group.querySelector('.ecl-help-block');
                if (helper) {
                    helper.setAttribute('id', `${this.inputId}-helper`);
                }
            }
        }
        if (this.eclScript) {
            const src = getAssetPath('./build/scripts/ecl-file-upload-vanilla.js');
            if (document.querySelector(`script[src="${src}"]`)) {
                document.querySelector(`script[src="${src}"]`).remove();
            }
            const script = document.createElement('script');
            script.src = src;
            script.onload = () => {
                const fileUpload = new ECL.FileUpload(this.el.firstElementChild);
                fileUpload.init();
            };
            // @ts-ignore
            const observer = new MutationObserver((mutationsList, observer) => {
                for (const mutation of mutationsList) {
                    if (mutation.type === 'childList') {
                        const nodes = mutation.addedNodes;
                        nodes.forEach(node => {
                            const htmlNode = node;
                            htmlNode.classList.add(`sc-ecl-file-upload-${this.theme}`);
                            const name = htmlNode.querySelector('.ecl-file-upload__item-name');
                            if (name) {
                                name.classList.add(`sc-ecl-file-upload-${this.theme}`);
                            }
                            const meta = htmlNode.querySelector('.ecl-file-upload__item-meta');
                            if (meta) {
                                meta.classList.add(`sc-ecl-file-upload-${this.theme}`);
                            }
                        });
                    }
                }
            });
            observer.observe(this.el.querySelector('.ecl-file-upload__list'), {
                attributes: true,
                childList: true,
                subtree: true,
            });
            document.body.appendChild(script);
        }
    }
    render() {
        const wrapperAttrs = {};
        wrapperAttrs['data-ecl-file-upload-group'] = true;
        const attributes = {
            class: this.getInputClasses(),
            type: this.type,
            id: this.inputId,
            name: this.name,
            required: this.required,
            disabled: this.disabled,
            placeholder: this.placeholder,
            'data-ecl-file-upload-input': true,
            multiple: this.multiple,
            value: this.defaultValue,
        };
        return (h("div", Object.assign({ class: this.getClass() }, wrapperAttrs), h("input", Object.assign({}, attributes)), h("label", { class: "ecl-file-upload__button-container", htmlFor: this.inputId }, h("span", { class: "ecl-file-upload__button ecl-button ecl-button--primary", "data-ecl-file-upload-button": true, "data-ecl-file-upload-label-choose": this.buttonChooseLabel, "data-ecl-file-upload-label-replace": this.buttonReplaceLabel }, this.buttonChooseLabel)), h("ul", { class: "ecl-file-upload__list", "data-ecl-file-upload-list": true })));
    }
    static get is() { return "ecl-file-upload"; }
    static get encapsulation() { return "scoped"; }
    static get originalStyleUrls() {
        return {
            "ec": ["./build/styles/ecl-file-upload-ec.css"],
            "eu": ["./build/styles/ecl-file-upload-eu.css"]
        };
    }
    static get styleUrls() {
        return {
            "ec": ["build/styles/ecl-file-upload-ec.css"],
            "eu": ["build/styles/ecl-file-upload-eu.css"]
        };
    }
    static get assetsDirs() { return ["build"]; }
    static get properties() {
        return {
            "theme": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "theme",
                "reflect": false,
                "defaultValue": "'ec'"
            },
            "styleClass": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "style-class",
                "reflect": false
            },
            "inputClass": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "input-class",
                "reflect": false
            },
            "eclScript": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "ecl-script",
                "reflect": false,
                "defaultValue": "false"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "disabled",
                "reflect": false,
                "defaultValue": "false"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "required",
                "reflect": false,
                "defaultValue": "false"
            },
            "invalid": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "invalid",
                "reflect": false,
                "defaultValue": "false"
            },
            "helperId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "helper-id",
                "reflect": false
            },
            "helperText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "helper-text",
                "reflect": false
            },
            "placeholder": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "placeholder",
                "reflect": false
            },
            "width": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "width",
                "reflect": false,
                "defaultValue": "'m'"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "label",
                "reflect": false
            },
            "type": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "type",
                "reflect": false
            },
            "inputId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "input-id",
                "reflect": false
            },
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "name",
                "reflect": false
            },
            "multiple": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "multiple",
                "reflect": false,
                "defaultValue": "false"
            },
            "defaultValue": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "default-value",
                "reflect": false
            },
            "buttonChooseLabel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "button-choose-label",
                "reflect": false
            },
            "buttonReplaceLabel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "attribute": "button-replace-label",
                "reflect": false
            }
        };
    }
    static get elementRef() { return "el"; }
}
//# sourceMappingURL=ecl-file-upload.js.map
