"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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TagsFilters = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var _ = __importStar(require("lodash"));
var use_deep_compare_effect_1 = require("use-deep-compare-effect");
var useMemoImmer_1 = require("../../../hooks/useMemoImmer");
var native_1 = require("../../Flex/native");
var RFValue_1 = require("../../RFValue");
var TextNative_1 = require("../../TextNative");
var TagsFilters_utils_1 = require("../TagsFilters.utils");
var TagsFilters_style_1 = require("./TagsFilters.style");
exports.TagsFilters = react_1.default.forwardRef(function TagsFilters(_a, ref) {
    var filters = _a.filters, filtersSchema = _a.filtersSchema, removeTag = _a.removeTag, onChange = _a.onChange;
    var tags = useMemoImmer_1.useMemoImmer(function () { return TagsFilters_utils_1.generateTagsFilters(filters, filtersSchema); }, [filters, filtersSchema]);
    react_1.useImperativeHandle(ref, function () { return ({
        tagsCount: tags.length,
    }); }, [tags]);
    use_deep_compare_effect_1.useDeepCompareEffectNoCheck(function () {
        if (onChange)
            onChange(tags);
    }, [tags]);
    var setEmptyField = react_1.useCallback(function setEmptyField(tag) {
        switch (tag.type) {
            case 'checkbox':
                removeTag(tag.key, _.remove(__spreadArrays(tag.values), function (n) { return n !== tag.checkboxValue; }));
                break;
            case 'location':
                removeTag(tag.key, tag.values
                    .split('_')
                    .filter(function (l) { return l !== tag.locationValue; })
                    .join('_'));
                break;
            case 'select':
            case 'input':
                removeTag(tag.key, tag.default);
                break;
        }
    }, []);
    return react_1.default.createElement(TagFiltersRender, { tags: tags, setEmptyField: setEmptyField });
});
var TagFiltersRender = react_1.default.memo(function TagFiltersRender(_a) {
    var tags = _a.tags, setEmptyField = _a.setEmptyField;
    return (react_1.default.createElement(TagsFilters_style_1.Container, { testID: "filters-tags" },
        react_1.default.createElement(native_1.Flex, null,
            react_1.default.createElement(react_native_1.ScrollView, { horizontal: true, showsHorizontalScrollIndicator: false },
                react_1.default.createElement(native_1.Flex, { horizontal: true, style: { paddingHorizontal: RFValue_1.RSValue(10) } }, !!tags.length &&
                    tags.map(function (tag, i) { return (react_1.default.createElement(TagFilter, { tag: tag, setEmptyField: setEmptyField, key: "tag-" + tag.value })); }))))));
}, _.isEqual);
var TagFilter = react_1.default.memo(function TagFilter(_a) {
    var tag = _a.tag, setEmptyField = _a.setEmptyField;
    return (react_1.default.createElement(TagsFilters_style_1.Tag, { testID: "tag-" + tag.value, onPress: function () { return setEmptyField(tag); }, ripple: true },
        react_1.default.createElement(TextNative_1.TextNative, { color: "white", size: 13 },
            tag.prefix,
            tag.value,
            tag.suffix),
        react_1.default.createElement(TagsFilters_style_1.Close, null, "\u00D7")));
}, _.isEqual);
//# sourceMappingURL=TagsFilters.js.map