"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTagsFilters = void 0;
var react_utils_1 = require("@devesharp/react-utils");
var date_fns_1 = require("date-fns");
var _ = __importStar(require("lodash"));
var TagsFilters_interface_1 = require("./TagsFilters.interface");
function generateTagsFilters(filters, filtersSchema) {
    var tags = Object.entries(filters || {})
        .filter(function (_a) {
        var key = _a[0];
        return filtersSchema[key];
    })
        .filter(function (_a) {
        var key = _a[0], value = _a[1];
        return (!react_utils_1.IsEmpty(value) && !_.isEqual(value, filtersSchema[key].default)) || value === 0 || value === '0';
    })
        .filter(function (_a) {
        var key = _a[0], value = _a[1];
        return filtersSchema[key].ignore
            ? filtersSchema[key].ignore.map(function (e) { return e.toString(); }).indexOf(filters[key].toString())
            : true;
    })
        .map(function (_a) {
        var _b, _c, _d, _e, _f, _g, _h;
        var key = _a[0], value = _a[1];
        var filtersSchemaKey = (_b = filtersSchema[key]) !== null && _b !== void 0 ? _b : {};
        var modifierValue = value;
        var calendarDate;
        switch (filtersSchemaKey.type) {
            case TagsFilters_interface_1.ETagFilterType.Select:
                return {
                    key: key,
                    type: filtersSchemaKey.type,
                    default: filtersSchemaKey.default || '',
                    value: filtersSchemaKey.values[filters[key]],
                };
            case TagsFilters_interface_1.ETagFilterType.Location:
                return value.split('_').map(function (i) {
                    var location = i.split(',');
                    var locationString;
                    if (location.length === 3) {
                        var estate = location[0], city = location[1], neighborhood = location[2];
                        locationString = neighborhood + " - " + city + ", " + estate;
                    }
                    else if (location.length === 2) {
                        var estate = location[0], city = location[1];
                        locationString = city + ", " + estate;
                    }
                    else {
                        return null;
                    }
                    return {
                        key: key,
                        type: 'location',
                        parentValue: filters[key],
                        value: locationString,
                        locationValue: i,
                        values: value,
                    };
                });
            case TagsFilters_interface_1.ETagFilterType.CheckBox:
                return value
                    .map(function (i) {
                    return filtersSchemaKey.values[i]
                        ? {
                            key: key,
                            type: 'checkbox',
                            default: false,
                            checkboxValue: i,
                            parentValue: filters[key],
                            value: filtersSchemaKey.values[i],
                            values: value,
                        }
                        : null;
                })
                    .filter(function (i) { return !react_utils_1.IsEmpty(i); });
            case TagsFilters_interface_1.ETagFilterType.Date:
                return {
                    key: key,
                    type: 'input',
                    prefix: (_c = filtersSchemaKey.prefix) !== null && _c !== void 0 ? _c : '',
                    suffix: (_d = filtersSchemaKey.suffix) !== null && _d !== void 0 ? _d : '',
                    value: date_fns_1.format(value, filtersSchemaKey.format || 'dd/MM/yyyy'),
                    default: (_e = filtersSchemaKey.default) !== null && _e !== void 0 ? _e : '',
                };
            case TagsFilters_interface_1.ETagFilterType.Input:
            case TagsFilters_interface_1.ETagFilterType.InputNumber:
            case TagsFilters_interface_1.ETagFilterType.InputPrice:
            default:
                if (filtersSchemaKey.type === 'price') {
                    var decimals = filtersSchemaKey.decimals || 2;
                    modifierValue = react_utils_1.NumberFormat(Number(modifierValue), decimals).toString();
                }
                if (filtersSchemaKey.type === 'number') {
                    var decimals = filtersSchemaKey.decimals || 0;
                    modifierValue = react_utils_1.NumberFormat(Number(modifierValue), decimals).toString();
                }
                return {
                    key: key,
                    type: 'input',
                    prefix: (_f = filtersSchemaKey.prefix) !== null && _f !== void 0 ? _f : '',
                    suffix: (_g = filtersSchemaKey.suffix) !== null && _g !== void 0 ? _g : '',
                    value: modifierValue,
                    default: (_h = filtersSchemaKey.default) !== null && _h !== void 0 ? _h : '',
                };
        }
    })
        .filter(function (i) { return !react_utils_1.IsEmpty(i); })
        .map(function (tag) { return tag; });
    return _.flattenDeep(tags);
}
exports.generateTagsFilters = generateTagsFilters;
//# sourceMappingURL=TagsFilters.utils.js.map