'use strict';

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

const core = require('./core-7e2b0104.js');
const utils = require('./utils-2a7c8f6e.js');
const i18n_provider = require('./i18n.provider-b7733c9d.js');

const BooleanFilterPanel = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.anjFilterChange = core.createEvent(this, "anjFilterChange", 7);
    }
    async reset() {
        this.element.querySelector('form').reset();
    }
    onSubmit(e) {
        e.preventDefault();
        const check = e.target.querySelector('input');
        this.anjFilterChange.emit({ field: this.field, type: 'equals', query: check.checked });
    }
    render() {
        return (core.h("form", { onSubmit: (e) => this.onSubmit(e) }, core.h("anj-checkbox-group", null, core.h("anj-checkbox", { name: "query" }, this.translator('Show only true'))), core.h("anj-button-group", { align: "right" }, core.h("anj-button", { context: "text", type: "submit" }, this.translator('Apply')))));
    }
    get element() { return core.getElement(this); }
    static get style() { return ".sc-anj-boolean-filter-panel-h{display:block;padding:1rem}.sc-anj-boolean-filter-panel-h anj-button-group.sc-anj-boolean-filter-panel{margin-bottom:0}"; }
};
i18n_provider.Tunnel.injectProps(BooleanFilterPanel, ['translator']);

const FILTER_TYPE_MAP = {
    'contains': '><',
    'ends-with': '<',
    'starts-with': '>',
    'equals': '='
};
const ColumnFilterToggle = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.filterListener = (e) => this.onFilter(e);
        this.toggleListener = (e) => this.onToggle(e);
        this.formatters = {
            date: new Intl.DateTimeFormat('en-US').format,
            datetime: new Intl.DateTimeFormat('en-US', i18n_provider.datetimeFormatOptions).format,
            number: new Intl.NumberFormat('en-US').format
        };
        this.active = false;
        this.anjFilter = core.createEvent(this, "anjFilter", 7);
    }
    injectedPropWatcher() {
        this.assignFormatters();
    }
    componentWillLoad() {
        this.table = this.element.closest('anj-table');
        this.table.addEventListener('anjColumnFilteringToggle', this.toggleListener);
        this.assignFormatters();
    }
    componentDidUnload() {
        this.table.removeEventListener('anjColumnFilteringToggle', this.toggleListener);
    }
    assignFormatters() {
        this.formatters.date = this.dateFormatter;
        this.formatters.datetime = this.datetimeFormatter;
        this.formatters.number = this.numberFormatter;
    }
    onToggle({ detail }) {
        this.active = detail;
    }
    async clearFilter(e) {
        e.preventDefault();
        e.stopPropagation();
        this.anjFilter.emit({ field: this.field, query: undefined, type: 'contains' });
        return this.panel.reset();
    }
    filterText() {
        const query = Array.isArray(this.filter.query) ?
            this.filter.query.map(this.formatValue.bind(this)).join(' - ') : this.formatValue(this.filter.query);
        return `${FILTER_TYPE_MAP[this.filter.type]} ${query}`;
    }
    bindPanel(el) {
        if (el) {
            el.addEventListener('anjFilterChange', this.filterListener);
        }
        else if (this.panel) {
            this.panel.removeEventListener('anjFilterChange', this.filterListener);
        }
        this.panel = el;
    }
    async onFilter({ detail }) {
        if (detail && (!this.filter || (detail.query !== this.filter.query || detail.type !== this.filter.type))) {
            this.anjFilter.emit(detail);
        }
        return this.dropdown.close();
    }
    formatValue(value) {
        return this.formatters[this.dataType] ? this.formatters[this.dataType](value) : value;
    }
    render() {
        const TagType = `anj-${this.dataType}-filter-panel`;
        const hostStyles = {
            height: this.active ? `${this.dropdown.offsetHeight}px` : '0',
            'margin-bottom': this.active ? '-0.657rem' : '0'
        };
        return (core.h(core.Host, { style: hostStyles, "aria-hidden": `${!this.active}` }, core.h("anj-dropdown", { ref: (r) => this.dropdown = r, arrow: false }, core.h("button", { class: `anj-column-filter-toggle anj-filter-toggle-${this.dataType}`, slot: "dropdown-toggle", tabIndex: this.active ? 0 : -1 }, this.filter ?
            [
                core.h("span", { class: "anj-has-value" }, this.filterText()),
                core.h("anj-button-close", { context: "clear", free: true, onClick: e => this.clearFilter(e) })
            ] :
            [
                core.h("svg", { xmlns: "http://www.w3.org/2000/svg", width: "14", height: "14", viewBox: "0 0 24 24" }, core.h("path", { fill: "currentColor", d: "M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" })),
                core.h("span", null, this.translator('Filter'))
            ]), core.h(TagType, { types: this.types, filter: this.filter, field: this.field, ref: el => this.bindPanel(el) }))));
    }
    get element() { return core.getElement(this); }
    static get watchers() { return {
        "translator": ["injectedPropWatcher"],
        "dateFormatter": ["injectedPropWatcher"],
        "numberFormatter": ["injectedPropWatcher"]
    }; }
    static get style() { return ".sc-anj-column-filter-toggle-h{bottom:-1px;display:block;margin-bottom:0;overflow:hidden;position:relative;-webkit-transition:height .4s ease,margin-bottom .4s ease;transition:height .4s ease,margin-bottom .4s ease}.sc-anj-column-filter-toggle-h anj-dropdown.sc-anj-column-filter-toggle{width:100%}.sc-anj-column-filter-toggle-h .anj-column-filter-toggle.sc-anj-column-filter-toggle{-ms-flex-align:center;align-items:center;background-color:transparent;border-color:transparent;border-radius:0;border-width:0 0 1px;color:var(--anj-neutral-dark);display:-ms-flexbox;display:flex;-ms-flex-pack:start;justify-content:flex-start;padding:0 0 .5rem;position:relative;text-align:inherit;-webkit-transition:border-color .15s;transition:border-color .15s;-webkit-appearance:button;width:100%}.sc-anj-column-filter-toggle-h .anj-column-filter-toggle.anj-filter-toggle-number.sc-anj-column-filter-toggle{-ms-flex-pack:end;justify-content:flex-end}.sc-anj-column-filter-toggle-h .anj-column-filter-toggle.sc-anj-column-filter-toggle:focus, .sc-anj-column-filter-toggle-h .anj-column-filter-toggle[aria-expanded=true].sc-anj-column-filter-toggle{border-color:var(--anj-highlight);outline:none}.sc-anj-column-filter-toggle-h .anj-column-filter-toggle.sc-anj-column-filter-toggle span.sc-anj-column-filter-toggle{display:block;max-width:100%;overflow:hidden;padding-left:.25rem;text-overflow:ellipsis;white-space:nowrap}.sc-anj-column-filter-toggle-h .anj-column-filter-toggle.sc-anj-column-filter-toggle span.anj-has-value.sc-anj-column-filter-toggle{color:var(--anj-text)}.sc-anj-column-filter-toggle-h anj-button-close.sc-anj-column-filter-toggle{-webkit-transform:scale(.75);transform:scale(.75)}"; }
};
i18n_provider.Tunnel.injectProps(ColumnFilterToggle, ['dateFormatter', 'datetimeFormatter', 'numberFormatter', 'translator']);

var Translations;
(function (Translations) {
    Translations["COLUMN_FILTERS"] = "Column Filters";
})(Translations || (Translations = {}));
const ColumnFiltering = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.active = false;
        this.anjColumnFilteringDidLoad = core.createEvent(this, "anjColumnFilteringDidLoad", 7);
        this.anjColumnFilteringToggle = core.createEvent(this, "anjColumnFilteringToggle", 7);
    }
    componentDidLoad() {
        this.anjColumnFilteringDidLoad.emit();
    }
    toggle() {
        this.active = !this.active;
        this.anjColumnFilteringToggle.emit(this.active);
    }
    render() {
        return (core.h("anj-button", { context: "icon", "aria-label": this.translator(Translations.COLUMN_FILTERS), active: this.active, onClick: () => this.toggle() }, core.h("svg", { slot: "icon-only", xmlns: "http://www.w3.org/2000/svg", width: "24", height: "24", viewBox: "0 0 24 24" }, core.h("path", { "fill-rule": "evenodd", d: "M15.0013226,19.88 C15.0413226,20.18 14.9413226,20.5 14.7113226,20.71 C14.3213226,21.1 13.6913226,21.1 13.3013226,20.71 L9.29132255,16.7 C9.06132255,16.47 8.96132255,16.16 9.00132255,15.87 L9.00132255,10.75 L4.21132255,4.62 C3.87132255,4.19 3.95132255,3.56 4.38132255,3.22 C4.57132255,3.08 4.78132255,3 5.00132255,3 L5.00132255,3 L19.0013226,3 L19.0013226,3 C19.2213226,3 19.4313226,3.08 19.6213226,3.22 C20.0513226,3.56 20.1313226,4.19 19.7913226,4.62 L15.0013226,10.75 L15.0013226,19.88 L15.0013226,19.88 Z M7.04132255,5 L11.0013226,10.06 L11.0013226,15.58 L13.0013226,17.58 L13.0013226,10.05 L16.9613226,5 L7.04132255,5 Z" }))));
    }
};
i18n_provider.Tunnel.injectProps(ColumnFiltering, ['translator']);

const DateFilterPanel = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.anjFilterChange = core.createEvent(this, "anjFilterChange", 7);
    }
    onChange({ detail, target }) {
        const input = target;
        this[input.name] = detail.innerValue;
    }
    async reset() {
        this.element.querySelector('form').reset();
        this.max = undefined;
        this.min = undefined;
    }
    onSubmit(e) {
        e.preventDefault();
        const valid = e.target.checkValidity();
        if (!valid)
            return;
        const filter = { field: this.field };
        let query;
        let type;
        if (this.min && this.max) {
            type = 'contains';
            query = [new Date(this.min), new Date(this.max)];
        }
        else if (this.min) {
            type = 'starts-with';
            query = new Date(this.min);
        }
        else {
            type = 'ends-with';
            query = new Date(this.max);
        }
        if (query) {
            filter.type = type;
            filter.query = query;
            this.anjFilterChange.emit(filter);
        }
    }
    render() {
        return (core.h("form", { onSubmit: (e) => this.onSubmit(e), novalidate: true }, this.types.includes('starts-with') || this.types.includes('contains') ?
            core.h("anj-form-group", null, core.h("anj-label", null, this.translator('Start Date')), core.h("anj-input", { name: "min", max: this.max }, core.h("anj-date-picker-toggle", null), core.h("anj-date-picker", null))) : undefined, this.types.includes('ends-with') || this.types.includes('contains') ?
            core.h("anj-form-group", null, core.h("anj-label", null, this.translator('End Date')), core.h("anj-input", { name: "max", min: this.min }, core.h("anj-date-picker-toggle", null), core.h("anj-date-picker", null))) : undefined, core.h("anj-button-group", { align: "right" }, core.h("anj-button", { context: "text", type: "submit" }, this.translator('Apply')))));
    }
    get element() { return core.getElement(this); }
    static get style() { return ".sc-anj-date-filter-panel-h{display:block;padding:1rem}.sc-anj-date-filter-panel-h anj-button-group.sc-anj-date-filter-panel{margin-bottom:0}"; }
};
i18n_provider.Tunnel.injectProps(DateFilterPanel, ['translator']);

const DatetimeFilterPanel = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.anjFilterChange = core.createEvent(this, "anjFilterChange", 7);
    }
    async reset() {
        this.element.querySelector('form').reset();
        this.max = undefined;
        this.min = undefined;
    }
    handleChanges({ detail, target }) {
        const input = target;
        this[input.name] = detail.value;
    }
    onSubmit(e) {
        e.preventDefault();
        const valid = e.target.checkValidity();
        if (!valid)
            return;
        const filter = { field: this.field };
        let query;
        let type;
        if (this.min && this.max) {
            type = 'contains';
            query = [new Date(this.min), new Date(this.max)];
        }
        else if (this.min) {
            type = 'starts-with';
            query = new Date(this.min);
        }
        else if (this.max) {
            type = 'ends-with';
            query = new Date(this.max);
        }
        else {
            this.anjFilterChange.emit(undefined);
        }
        if (query) {
            filter.type = type;
            filter.query = query;
            this.anjFilterChange.emit(filter);
        }
    }
    render() {
        return (core.h("form", { onSubmit: (e) => this.onSubmit(e), novalidate: true }, this.types.includes('starts-with') || this.types.includes('contains') ?
            core.h("anj-form-group", null, core.h("anj-label", null, this.translator('Start Date')), core.h("anj-input", { name: "min", max: this.max, onAnjChange: e => this.handleChanges(e) }, core.h("anj-datetime-picker-toggle", null), core.h("anj-datetime-picker", { nested: true }))) : undefined, this.types.includes('ends-with') || this.types.includes('contains') ?
            core.h("anj-form-group", null, core.h("anj-label", null, this.translator('End Date')), core.h("anj-input", { name: "max", min: this.min, onAnjChange: e => this.handleChanges(e) }, core.h("anj-datetime-picker-toggle", null), core.h("anj-datetime-picker", { nested: true }))) : undefined, core.h("anj-button-group", { align: "right" }, core.h("anj-button", { context: "text", type: "submit" }, this.translator('Apply')))));
    }
    get element() { return core.getElement(this); }
    static get style() { return ".sc-anj-datetime-filter-panel-h{display:block;padding:1rem}.sc-anj-datetime-filter-panel-h anj-button-group.sc-anj-datetime-filter-panel{margin-bottom:0}"; }
};
i18n_provider.Tunnel.injectProps(DatetimeFilterPanel, ['translator']);

const NumberFilterPanel = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.anjFilterChange = core.createEvent(this, "anjFilterChange", 7);
    }
    onChange({ detail, target }) {
        const el = target;
        this[el.name] = detail.checked ? detail.checked : detail.value;
    }
    async reset() {
        this.element.querySelector('form').reset();
        this.max = undefined;
        this.min = undefined;
    }
    onSubmit(e) {
        e.preventDefault();
        const filter = { field: this.field };
        const valid = e.target.checkValidity();
        if (!valid)
            return;
        let query;
        let type;
        if (utils.hasValue(this.min) && utils.hasValue(this.max)) {
            type = 'contains';
            query = [parseInt(this.min, 10), parseInt(this.max, 10)];
        }
        else if (utils.hasValue(this.min)) {
            type = 'starts-with';
            query = parseInt(this.min, 10);
        }
        else {
            type = 'ends-with';
            query = parseInt(this.max, 10);
        }
        if (utils.hasValue(query)) {
            filter.type = type;
            filter.query = query;
            this.anjFilterChange.emit(filter);
        }
    }
    render() {
        return (core.h("form", { onSubmit: (e) => this.onSubmit(e), novalidate: true }, this.types.includes('starts-with') || this.types.includes('contains') ?
            core.h("anj-form-group", null, core.h("anj-label", null, this.translator('Min')), core.h("anj-input", { name: "min", type: "number", max: this.max, value: this.min })) : undefined, this.types.includes('ends-with') || this.types.includes('contains') ?
            core.h("anj-form-group", null, core.h("anj-label", null, this.translator('Max')), core.h("anj-input", { name: "max", type: "number", min: this.min, value: this.max })) : undefined, core.h("anj-button-group", { align: "right" }, core.h("anj-button", { context: "text", type: "submit" }, this.translator('Apply')))));
    }
    get element() { return core.getElement(this); }
    static get style() { return ".sc-anj-number-filter-panel-h{display:block;padding:1rem}.sc-anj-number-filter-panel-h anj-button-group.sc-anj-number-filter-panel{margin-bottom:0}"; }
};
i18n_provider.Tunnel.injectProps(NumberFilterPanel, ['translator']);

var FilterTypeLabels;
(function (FilterTypeLabels) {
    FilterTypeLabels["contains"] = "Contains";
    FilterTypeLabels["ends-with"] = "Ends With";
    FilterTypeLabels["equals"] = "Equals";
    FilterTypeLabels["starts-with"] = "Starts With";
})(FilterTypeLabels || (FilterTypeLabels = {}));
const StringFilterPanel = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.anjFilterChange = core.createEvent(this, "anjFilterChange", 7);
    }
    async reset() {
        this.element.querySelector('form').reset();
    }
    componentWillLoad() {
        this.type = this.filter ? this.filter.type : this.types[0] || 'contains';
    }
    onSubmit(e) {
        e.preventDefault();
        const filter = {};
        Array.from(e.target.querySelectorAll('input')).forEach((i) => {
            filter[i.name] = i.value.trim() !== '' ? i.value.trim() : undefined;
        });
        filter.type = this.type;
        this.anjFilterChange.emit(filter);
    }
    render() {
        return (core.h("form", { onSubmit: (e) => this.onSubmit(e) }, core.h("input", { type: "hidden", name: "field", value: this.field }), this.types.length > 1 ?
            core.h("anj-form-group", null, core.h("anj-label", null, this.translator('Type')), core.h("anj-select", { name: "type", value: this.type, onAnjChange: e => this.type = e.detail.value }, this.types.map(t => core.h("anj-option", { value: t }, this.translator(FilterTypeLabels[t])))))
            : undefined, core.h("anj-form-group", null, core.h("anj-label", null, this.translator('Filter')), core.h("anj-input", { name: "query" })), core.h("anj-button-group", { align: "right" }, core.h("anj-button", { context: "text", type: "submit" }, this.translator('Apply')))));
    }
    get element() { return core.getElement(this); }
    static get style() { return ".sc-anj-string-filter-panel-h{display:block;padding:1rem}.sc-anj-string-filter-panel-h anj-button-group.sc-anj-string-filter-panel{margin-bottom:0}"; }
};
i18n_provider.Tunnel.injectProps(StringFilterPanel, ['translator']);

exports.anj_boolean_filter_panel = BooleanFilterPanel;
exports.anj_column_filter_toggle = ColumnFilterToggle;
exports.anj_column_filtering = ColumnFiltering;
exports.anj_date_filter_panel = DateFilterPanel;
exports.anj_datetime_filter_panel = DatetimeFilterPanel;
exports.anj_number_filter_panel = NumberFilterPanel;
exports.anj_string_filter_panel = StringFilterPanel;
