var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import { bindable, bindingMode, customElement, children, inlineView, ViewCompiler, ViewResources } from "aurelia-framework";
import { BindingEngine, Parser } from 'aurelia-binding';
import { CommonColumn } from "./common-column";
export var UITableAction = (function (_super) {
    __extends(UITableAction, _super);
    function UITableAction(element, parser, viewCompiler, viewResources, bindingEngine) {
        var _this = _super.call(this, element, parser, viewCompiler, viewResources, bindingEngine) || this;
        _this.actions = [];
        _this.theme = "primary";
        _this.icon = "digi-three-points-vertical";
        _this.disabledMenuItems = false;
        _this.useDropdown = true;
        _this.cellAlign = "center";
        return _this;
    }
    UITableAction.prototype.compileTemplate = function (currentRecord) {
        var menuItemsBodyCell = [];
        var actionsFiltered = this.actions.filter(function (a) { return a.isVisible(currentRecord); });
        this.templateCell = document.createElement("template");
        if (actionsFiltered.length == 1 || (false == this.useDropdown && actionsFiltered.length >= 1)) {
            var buttonsHtml = actionsFiltered.map(function (button) {
                if (button.label != null) {
                    button.element.setAttribute("t", "[title]" + button.label);
                }
                if (button.element.attributes["click.delegate"] == null) {
                    var functionName = button.element.attributes["click.call"].value.split("(")[0];
                    button.element.setAttribute("click.delegate", functionName + "($this)");
                    button.element.removeAttribute("click.call");
                }
                return button.element.outerHTML;
            });
            this.templateCell.innerHTML = buttonsHtml.join('');
        }
        else if (actionsFiltered.length > 1) {
            this.templateCell.innerHTML =
                "<ui-button disabled.bind=\"column.disabledMenuItems\" icon=\"${column.icon}\" ui-theme=\"${column.theme}\" type=\"tool\" menu-items.bind=\"menuItemsBodyCell\" show-caret.bind=\"false\" current.bind=\"currentRecord\"></ui-button>";
            menuItemsBodyCell.push.apply(menuItemsBodyCell, __spreadArray([], __read(actionsFiltered.map(function (action) {
                var _a;
                if (action.menuItems == null || ((_a = action.menuItems) === null || _a === void 0 ? void 0 : _a.length) == 0) {
                    return {
                        label: action.label,
                        icon: action.icon,
                        handler: function (record) {
                            action.element.click({ record: record });
                        },
                        disabled: function () {
                            if (typeof (action.disabled) == 'function') {
                                return action.disabled(currentRecord);
                            }
                            else if (typeof (action.disabled) == 'boolean') {
                                return action.disabled;
                            }
                        },
                    };
                }
                else {
                    return {
                        label: action.label,
                        icon: action.icon,
                        items: action.menuItems,
                        disabled: function () {
                            if (typeof (action.disabled) == 'function') {
                                return action.disabled(currentRecord);
                            }
                            else if (typeof (action.disabled) == 'boolean') {
                                return action.disabled;
                            }
                        },
                    };
                }
            })), false));
        }
        return menuItemsBodyCell;
    };
    __decorate([
        children('ui-button'),
        __metadata("design:type", Array)
    ], UITableAction.prototype, "actions", void 0);
    __decorate([
        bindable({ defaultBindingMode: bindingMode.toView }),
        __metadata("design:type", String)
    ], UITableAction.prototype, "theme", void 0);
    __decorate([
        bindable({ defaultBindingMode: bindingMode.toView }),
        __metadata("design:type", String)
    ], UITableAction.prototype, "icon", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Object)
    ], UITableAction.prototype, "disabledMenuItems", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], UITableAction.prototype, "useDropdown", void 0);
    UITableAction = __decorate([
        customElement("ui-table-action"),
        inlineView("\n  <template>\n    <slot></slot>\n  </template>\n"),
        __metadata("design:paramtypes", [Element, Parser, ViewCompiler, ViewResources, BindingEngine])
    ], UITableAction);
    return UITableAction;
}(CommonColumn));

//# sourceMappingURL=ui-table-action.js.map
