var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(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 { h } from './core-15e24a78.js';
import { d as concatClasses, j as hasValue } from './utils-3aef1637.js';
var COLUMN_CONTEXT_MENU = [
    {
        label: 'Pin Left',
        value: 'left'
    },
    {
        label: 'Pin Right',
        value: 'right'
    },
    {
        label: 'Unpin',
        value: null
    }
];
var Translations;
(function (Translations) {
    Translations["SELECT"] = "Select";
    Translations["DESELECT"] = "Deselect";
})(Translations || (Translations = {}));
var FILTER_TYPES = ['contains', 'ends-with', 'equals', 'starts-with'];
var ColumnClasses;
(function (ColumnClasses) {
    ColumnClasses["cell"] = "anj-cell";
    ColumnClasses["header"] = "anj-header-cell";
    ColumnClasses["number"] = "anj-datatype-number";
    ColumnClasses["overflowCell"] = "anj-overflow-cell";
    ColumnClasses["overflowHeader"] = "anj-overflow-header";
    ColumnClasses["pinBorder"] = "anj-pin-border";
    ColumnClasses["selectable"] = "anj-selectable";
    ColumnClasses["selected"] = "anj-selected-col";
    ColumnClasses["sorted"] = "anj-sorted";
})(ColumnClasses || (ColumnClasses = {}));
var colId = 0;
var ColumnRenderer = /** @class */ (function () {
    function ColumnRenderer(column) {
        this.column = column;
    }
    ColumnRenderer.classFromColumnName = function (name) {
        return "anj-col-" + name.replace(/\//g, '-');
    };
    ColumnRenderer.classFromPinned = function (pinned) {
        if (!pinned) {
            return;
        }
        return "anj-pinned-" + pinned;
    };
    ColumnRenderer.pinnedBorderClass = function (index, boundaries) {
        return index === boundaries.lastLeftPinned || index === boundaries.firstRightPinned ? ColumnClasses.pinBorder : undefined;
    };
    ColumnRenderer.columnHeaderClasses = function (column) {
        return concatClasses(ColumnClasses.header, column.sortIndex > -1 ? ColumnClasses.sorted : undefined, column.overflow === 'all' || column.overflow === 'header' ? ColumnClasses.overflowHeader : undefined, ColumnRenderer.columnClasses(column));
    };
    ColumnRenderer.columnCellClasses = function (column) {
        return concatClasses(ColumnClasses.cell, column.overflow === 'all' || column.overflow === 'cell' ? ColumnClasses.overflowCell : undefined, ColumnRenderer.columnClasses(column));
    };
    ColumnRenderer.columnAttributes = function (column, cIndex, row) {
        return {
            colspan: column.span,
            'data-column-index': cIndex,
            'data-column-field': column.field,
            'data-row-key': row[column.table.rowKey]
        };
    };
    ColumnRenderer.columnClasses = function (column) {
        return [
            ColumnRenderer.classFromColumnName(column.name),
            ColumnRenderer.classFromPinned(column.pinned),
            column.table.selectedColumnFields.includes(column.field) ? ColumnClasses.selected : undefined,
            column.selectable ? ColumnClasses.selectable : undefined,
            "anj-datatype-" + column.dataType
        ];
    };
    ColumnRenderer.generateColumnName = function (field, caption) {
        var name = field ? field : caption ? caption.split(' ').join('-') : "anj-anon-" + colId++;
        return name.toLowerCase();
    };
    ColumnRenderer.renderCell = function (data, column) {
        if (!hasValue(data[column.field])) {
            return '';
        }
        switch (column.dataType) {
            case 'boolean':
                return booleanRenderer(data[column.field]);
            case 'date':
                return dateRenderer(data[column.field], column);
            case 'datetime':
                return datetimeRenderer(data[column.field], column);
            case 'number':
                return numberRenderer(data[column.field], column);
            default:
                return typeof data[column.field] === 'string' ? "" + data[column.field].substring(0, 999) : "" + data[column.field];
        }
    };
    ColumnRenderer.renderEdit = function (data, column) {
        var translator = this.translator || ColumnRenderer.translator;
        var attrs = ColumnRenderer.validationAttrs(column.rules);
        var placeholder = ColumnRenderer.renderHeader(column.field, column.caption);
        if (column.options) {
            return (h("anj-form-group", null, h("anj-select", Object.assign({}, attrs, { placeholder: placeholder, name: column.field, value: data[column.field] }), column.options.options.map(function (o) { return h("anj-option", { value: column.options.valueKey ? o[column.options.valueKey] : o }, column.options.displayKey ? o[column.options.displayKey] : o); }))));
        }
        switch (column.dataType) {
            case 'boolean':
                var label = data[column.field]
                    ? translator(Translations.DESELECT) + ": " + column.name
                    : translator(Translations.SELECT) + ": " + column.name;
                return h("anj-checkbox", { solo: true, checked: data[column.field], name: column.field, "aria-label": label });
            case 'date':
                return (h("anj-form-group", null, h("anj-input", Object.assign({}, attrs, { placeholder: placeholder, name: column.field, value: dateRenderer(data[column.field], column) }), h("anj-date-picker-toggle", null), h("anj-date-picker", null))));
            case 'datetime':
                return (h("anj-form-group", null, h("anj-input", Object.assign({}, attrs, { placeholder: placeholder, name: column.field, value: datetimeRenderer(data[column.field], column) }), h("anj-datetime-picker-toggle", null), h("anj-datetime-picker", null))));
            default:
                var type = attrs.type ? attrs.type : column.dataType === 'string' ? 'text' : column.dataType;
                var value = hasValue(data[column.field]) ? data[column.field] : '';
                return (h("anj-form-group", null, h("anj-input", Object.assign({}, attrs, { placeholder: placeholder, name: column.field, type: type, value: value }))));
        }
    };
    ColumnRenderer.validationAttrs = function (rules) {
        var attrs = {};
        rules.forEach(function (rule) {
            switch (rule.type) {
                case 'required':
                    attrs['required'] = true;
                    break;
                case 'email':
                    attrs['type'] = 'email';
                    break;
                default:
                    attrs[rule.type] = rule.value;
            }
        });
        return attrs;
    };
    ColumnRenderer.renderHeader = function (field, caption) {
        return caption ? caption : ColumnRenderer.fieldToCaption(field);
    };
    ColumnRenderer.fieldToCaption = function (field) {
        return field ? field.split(/(?=[A-Z])/).join(' ').replace(/_/g, ' ') : '';
    };
    ColumnRenderer.prototype.contextMenu = function () {
        return __awaiter(this, void 0, void 0, function () {
            var options, updated;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!this.column.pinnable)
                            return [2 /*return*/, undefined];
                        return [4 /*yield*/, this.column.table.columnMenu()];
                    case 1:
                        options = _a.sent();
                        updated = options.map(function (o) {
                            o.disabled = o.action === _this.column.pinned;
                            return o;
                        });
                        return [2 /*return*/, updated.length > 0 ? updated : undefined];
                }
            });
        });
    };
    ColumnRenderer.prototype.setOffset = function (offset) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                this.cells().forEach(function (cell) { return _this.setCellOffset(cell, offset); });
                return [2 /*return*/];
            });
        });
    };
    ColumnRenderer.prototype.setWidth = function (width) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                this.column.width = width;
                this.cells().forEach(function (cell) { return _this.setCellWidth(cell); });
                return [2 /*return*/];
            });
        });
    };
    ColumnRenderer.prototype.initCell = function (cell, offset) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                if (!cell) {
                    return [2 /*return*/];
                }
                this.setCellOffset(cell, offset);
                if (this.column.width !== 'auto') {
                    this.setCellWidth(cell);
                }
                return [2 /*return*/];
            });
        });
    };
    ColumnRenderer.prototype.cells = function () {
        return Array.from(this.column.table.querySelectorAll("." + ColumnRenderer.classFromColumnName(this.column.name)));
    };
    ColumnRenderer.prototype.setCellOffset = function (cell, offset) {
        switch (this.column.pinned) {
            case 'left':
                cell.style.left = offset ? offset : this.calculateOffset(cell) + "px";
                break;
            case 'right':
                cell.style.right = offset ? offset : this.calculateOffset(cell, true) + "px";
                break;
            default:
                cell.style.left = '';
                cell.style.right = '';
                break;
        }
    };
    ColumnRenderer.prototype.setCellWidth = function (cell) {
        cell.style.minWidth = this.column.width === 'auto' || this.column.width === '0' ? '' : this.column.width;
        cell.style.maxWidth = this.column.width === 'auto' || this.column.width === '0' ? '' : this.column.width;
        cell.style.width = this.column.width;
    };
    ColumnRenderer.prototype.calculateOffset = function (cell, isRight) {
        if (isRight === void 0) { isRight = false; }
        var offset = 0;
        var pinned = Array.from(cell.parentElement.querySelectorAll("." + ColumnRenderer.classFromPinned(this.column.pinned)));
        if (isRight) {
            pinned.reverse();
        }
        for (var i = 0; i < pinned.length; i++) {
            if (pinned[i] === cell) {
                break;
            }
            offset = offset + pinned[i].offsetWidth;
        }
        return offset;
    };
    return ColumnRenderer;
}());
ColumnRenderer.translator = function (s, _v) { return s; };
var booleanRenderer = function (value) {
    return value ? h("svg", { xmlns: "http://www.w3.org/2000/svg", width: "18", height: "18", viewBox: "0 0 18 18" }, h("path", { d: "M6.61 11.89L3.5 8.78 2.44 9.84 6.61 14l8.95-8.95L14.5 4z" })) : '';
};
var datetimeRenderer = function (value, column) {
    var dateValue = new Date(value);
    return dateValue.valueOf() ? column.table.datetimeFormatter(dateValue) : '';
};
var dateRenderer = function (value, column) {
    var dateValue = new Date(value);
    return dateValue.valueOf() ? column.table.dateFormatter(dateValue) : '';
};
var numberRenderer = function (value, column) {
    if (!hasValue(value))
        return '';
    if (isNaN(value))
        return value;
    return !isNaN(value) ? column.table.numberFormatter(value) : value;
};
export { ColumnRenderer as C, FILTER_TYPES as F, COLUMN_CONTEXT_MENU as a };
