var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { r as registerInstance, c as createEvent, h, g as getElement } from './core-095fd691.js';
import { N as NativeEvent } from './native-event-23d5d7d1.js';
var BceOptionCreator = /** @class */ (function () {
    function class_1(hostRef) {
        var _this = this;
        registerInstance(this, hostRef);
        this.value = [];
        this.allowImage = true;
        this.allowColor = false;
        this.allowOther = false;
        this.other = false;
        this.labelOCAddButton = 'Add';
        this.labelOCRemoveButton = 'Delete';
        this.labelOCOther = 'Other';
        this.labelOCImage = 'Image';
        this.handleInput = function (event, index) {
            event.cancelBubble = true;
            var input = event.target;
            if (!input)
                return;
            // Update value
            var copy = _this.value.slice();
            var update = Object.assign({}, _this.value[index], { text: input.value });
            copy.splice(index, 1, update);
            _this.value = copy;
            _this.el.dispatchEvent(new NativeEvent('input'));
        };
        this.handleKeyDown = function (event) { return __awaiter(_this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                if (event.code === 'Enter')
                    this.handleAdd();
                return [2 /*return*/];
            });
        }); };
        this.handleAdd = function (focus) {
            if (focus === void 0) { focus = true; }
            var insert = { text: '' };
            _this.value = _this.other
                ? _this.value.slice(0, -1).concat([insert], _this.value.slice(-1)) : _this.value.concat([insert]);
            if (focus)
                _this.focusLast(true);
        };
        this.handleOther = function () {
            _this.other = !_this.other;
            _this.value = _this.other
                ? _this.value.concat([{ text: _this.labelOCOther }]) : _this.value.slice(0, -1);
            _this.onOther.emit(_this.other);
        };
        this.handleRemove = function (index) {
            var copy = _this.value.slice();
            copy.splice(index, 1);
            _this.value = copy;
            _this.el.dispatchEvent(new NativeEvent('input'));
        };
        this.handleColor = function (index) { };
        this.handleImage = function (index) {
            var current = _this.value[index];
            if ('image' in current && current.image)
                _this.deleteImage(current, index);
            else
                _this.uploadImage(index);
        };
        this.onOther = createEvent(this, "other", 7);
        this.onFile = createEvent(this, "file", 7);
        this.onFileDelete = createEvent(this, "file-delete", 7);
    }
    class_1.prototype.watchValue = function (value) {
        if (!value.length)
            this.handleAdd(false);
    };
    class_1.prototype.focusLast = function (nextTick) {
        if (nextTick === void 0) { nextTick = false; }
        return __awaiter(this, void 0, void 0, function () {
            var options, option, input;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!nextTick) return [3 /*break*/, 2];
                        return [4 /*yield*/, new Promise(function (res) { return setTimeout(res, 100); })];
                    case 1:
                        _a.sent();
                        _a.label = 2;
                    case 2:
                        options = this.el.querySelectorAll('[data-option]');
                        option = options[options.length - (this.other ? 2 : 1)];
                        input = option && option.querySelector('bce-input input');
                        // Set focus to new element
                        if (input)
                            input.focus();
                        return [2 /*return*/];
                }
            });
        });
    };
    class_1.prototype.uploadImage = function (index) {
        return __awaiter(this, void 0, void 0, function () {
            var root, input, file, copy;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        root = this.el.closest('bce-root');
                        input = document.createElement('bce-upload-input');
                        return [4 /*yield*/, root.execute(input, function (res) {
                                input.addEventListener('file', function (e) {
                                    e.cancelBubble = true;
                                    res(e.detail);
                                });
                                input.upload();
                            })];
                    case 1:
                        file = _a.sent();
                        // Emit file
                        this.onFile.emit(file);
                        copy = this.value.slice();
                        copy.splice(index, 1, Object.assign({}, this.value[index], { image: file.path }));
                        // Emit value
                        this.value = copy;
                        this.el.dispatchEvent(new NativeEvent('input'));
                        return [2 /*return*/];
                }
            });
        });
    };
    class_1.prototype.deleteImage = function (option, index) {
        if (!('image' in option) || !option.image)
            return;
        // Emit file delete
        var match = option.image.match(/BCE:\/\/([^/]+)\//);
        if (match)
            this.onFileDelete.emit({ id: match[1], path: option.image });
        // Update value
        var copy = this.value.slice();
        var value = Object.assign({}, this.value[index]);
        if ('image' in value)
            delete value.image;
        copy.splice(index, 1, value);
        // Emit value
        this.value = copy;
        this.el.dispatchEvent(new NativeEvent('input'));
    };
    class_1.prototype.componentDidLoad = function () {
        this.watchValue(this.value);
    };
    class_1.prototype.renderTooltip = function (icon, content) {
        if (!content)
            return null;
        return (h("bce-tooltip", { placement: "left", icon: icon }, content));
    };
    class_1.prototype.renderMetadata = function (option) {
        if (!('image' in option) || !option.image)
            return;
        return h("div", { "data-meta": true }, this.renderTooltip('fas:image', option.image));
    };
    class_1.prototype.renderOption = function (option, index, other) {
        var _this = this;
        if (other === void 0) { other = false; }
        if (!option.hasOwnProperty('text'))
            return null;
        var placeholder = this.placeholder && this.placeholder + " " + (index + 1);
        var icon = this.other
            ? 'fas:font'
            : Array.isArray(this.icon)
                ? this.icon[index]
                : this.icon;
        return (h("div", { key: index, "data-option": true }, h("bce-input", { value: option.text, onInput: function (e) { return _this.handleInput(e, index); }, onKeyDown: this.handleKeyDown, placeholder: placeholder }), h("bce-icon", { raw: icon, "fixed-width": true }), this.renderMetadata(option), h("bce-dropdown-menu", null, h("slot", { name: "option" }), this.allowColor && (h("bce-button", { icon: "fas:palette", onClick: function () { return _this.handleColor(index); } }, "Color")), this.allowImage && !other && (h("bce-button", { icon: "fas:image", onClick: function () { return _this.handleImage(index); } }, 'image' in option && option.image
            ? this.labelOCRemoveButton
            : this.labelOCAddButton, "\u00A0", this.labelOCImage)), this.value.length > 1 && (h("bce-button", { icon: "fas:trash", onClick: function () { return _this.handleRemove(index); } }, this.labelOCRemoveButton)))));
    };
    class_1.prototype.render = function () {
        var _this = this;
        var options = this.other ? this.value.slice(0, -1) : this.value;
        var other = this.other && this.value[this.value.length - 1];
        return [
            this.label && h("label", null, this.label),
            options.map(function (option, i) { return _this.renderOption(option, i); }),
            other && this.renderOption(other, this.value.length - 1, true),
            h("slot", null),
            h("div", null, h("bce-button", { type: "text", icon: "fas:plus", onClick: function () { return _this.handleAdd(); } }, this.labelOCAddButton, " ", this.placeholder), this.allowOther && (h("bce-button", { type: "text", icon: "fas:font", onClick: this.handleOther }, this.other ? this.labelOCRemoveButton : this.labelOCAddButton, "\u00A0\"", this.labelOCOther, "\"")), h("slot", { name: "action" }))
        ];
    };
    Object.defineProperty(class_1.prototype, "el", {
        get: function () { return getElement(this); },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(class_1, "watchers", {
        get: function () {
            return {
                "value": ["watchValue"]
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(class_1, "style", {
        get: function () { return "bce-option-creator{position:relative;display:block;margin-bottom:40px}bce-option-creator bce-icon,bce-option-creator bce-input{--bce-input-height:40px;z-index:2}bce-option-creator>:last-child{position:absolute;left:16px;bottom:-32px}bce-option-creator [data-option]{position:relative}bce-option-creator [data-option]>bce-input{margin:5px 0}bce-option-creator [data-option]>bce-input input{padding:13px 38px}bce-option-creator [data-option]>bce-icon{position:absolute;top:50%;left:10px;opacity:.6;-webkit-transform:translateY(-50%);transform:translateY(-50%)}bce-option-creator [data-option]>[data-meta]{position:absolute;right:30px;z-index:3;bottom:0;padding:4px}bce-option-creator [data-option]>[data-meta] bce-icon{font-size:14px;opacity:.5}bce-option-creator [data-option]>bce-dropdown-menu{position:absolute;right:10px;top:50%;-webkit-transform:translateY(-50%);transform:translateY(-50%);z-index:3}\@media screen and (min-width:1024px){bce-option-creator [data-option]>bce-input:not([compact]) input{padding:6px 38px}}"; },
        enumerable: true,
        configurable: true
    });
    return class_1;
}());
export { BceOptionCreator as bce_option_creator };
