'use strict';

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

const core = require('./core-906e2cc9.js');
require('./_commonjsHelpers-72d386ba.js');
const index = require('./index-d83d7645.js');

const SmoothlyAddresses = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.allowed = [];
        this.editing = undefined;
        this.change = core.createEvent(this, "change", 7);
    }
    get allowedArray() {
        if (!this.allowedArrayValue)
            this.allowedArrayValue = Array.isArray(this.allowed) ? this.allowed : this.allowed.split(" ");
        return this.allowedArrayValue;
    }
    get address() {
        if (typeof (this.value) == "string")
            this.value = JSON.parse(this.value);
        return this.value;
    }
    get addresses() { return index.dist_1.is(this.address) ? { primary: this.address } : Object.assign({}, this.address); }
    onChange(e) {
        if (index.dist_1.is(e.detail)) {
            this.editing = undefined;
            const type = e.detail.type;
            delete (e.detail.type);
            let result;
            if (type == "primary" && index.dist_1.is(this.address))
                result = Object.assign({}, e.detail);
            else {
                result = Object.assign({}, this.addresses);
                result[type] = e.detail;
            }
            this.change.emit(this.value = result);
        }
    }
    onTrigger(e) {
        if (e.detail.name == "add" && typeof (this.value) != "string") {
            const result = this.addresses;
            const added = index.dist_1.create("SE");
            const type = e.detail.value;
            result[type] = added;
            this.value = result;
            this.editing = type;
        }
    }
    render() {
        const allowed = this.allowedArray.filter(type => index.dist_2.is(this.address) ? this.address[type] == undefined : type != "primary");
        return [
            index.dist_1.is(this.address) ? core.h("smoothly-address", { editable: this.editable, value: this.address, onChange: (e) => this.onChange(e) }) :
                index.dist_2.map(this.address, (type, address) => core.h("smoothly-address", { mode: type == this.editing ? "edit" : "display", editable: this.editable, type: type, value: address, onChange: (e) => this.onChange(e) })),
            allowed.map(type => core.h("smoothly-trigger", { color: "primary", fill: "clear", name: "add", value: type }, "+", type)),
        ];
    }
    static get style() { return ".sc-smoothly-addresses-h{display:block}smoothly-trigger.sc-smoothly-addresses{display:inline-block;margin:5px}"; }
};

exports.smoothly_addresses = SmoothlyAddresses;
