"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tabs = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var styled_components_1 = require("styled-components");
var RFValue_1 = require("../../RFValue");
var Tabs_style_1 = require("./Tabs.style");
exports.Tabs = react_1.default.forwardRef(function Tabs(_a, ref) {
    var _b, _c;
    var options = _a.options, onSelect = _a.onSelect, _d = _a.disabled, disabled = _d === void 0 ? false : _d, _e = _a.skeleton, skeleton = _e === void 0 ? false : _e, defaultTab = _a.defaultTab;
    var theme = styled_components_1.useTheme();
    var indexDefault = react_1.useMemo(function () { return options.findIndex(function (i) { return i.id === defaultTab; }); }, [options]);
    var _f = react_1.useState(indexDefault === -1 ? 0 : indexDefault), selectTabIndex = _f[0], setSelectTabIndex = _f[1];
    var scrollViewRef = react_1.useRef();
    var layoutTabsRef = react_1.useRef({});
    var animatedwidth = react_1.useRef(new react_native_1.Animated.Value(-8000)).current;
    var animatedTrans = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var setTab = react_1.useCallback(function setTab(item, indexTab, runCallback) {
        if (runCallback === void 0) { runCallback = true; }
        setSelectTabIndex(function (currentSelect) {
            if (currentSelect !== indexTab) {
                if (runCallback) {
                    onSelect(options[indexTab]);
                }
                return indexTab;
            }
            return currentSelect;
        });
    }, []);
    var onPressTab = react_1.useCallback(function (tab, index, layoutRef, runCallback) {
        var _a, _b;
        if (runCallback === void 0) { runCallback = true; }
        if (layoutRef) {
            react_native_1.Animated.timing(animatedwidth, {
                toValue: layoutRef.width - 8000,
                duration: 200,
                useNativeDriver: true,
            }).start();
            react_native_1.Animated.timing(animatedTrans, {
                toValue: layoutRef.x,
                duration: 200,
                useNativeDriver: true,
            }).start();
            var scrollX_1 = layoutRef.x - react_native_1.Dimensions.get('window').width / 2 + layoutRef.width / 2;
            (_b = (_a = scrollViewRef === null || scrollViewRef === void 0 ? void 0 : scrollViewRef.current) === null || _a === void 0 ? void 0 : _a.getScrollResponder()) === null || _b === void 0 ? void 0 : _b.scrollTo({ x: scrollX_1, y: 0, animated: true });
        }
        setTab(tab, index, runCallback);
    }, []);
    react_1.useImperativeHandle(ref, function () { return ({
        setTab: function (tab) {
            var indexTab = options.findIndex(function (i) { return i.id === tab; });
            if (indexTab !== -1) {
                onPressTab(options[indexTab], indexTab, layoutTabsRef.current[indexTab], false);
            }
        },
    }); }, []);
    return (react_1.default.createElement(Tabs_style_1.Container, null,
        react_1.default.createElement(react_native_1.ScrollView, { ref: scrollViewRef, horizontal: true, showsHorizontalScrollIndicator: false },
            react_1.default.createElement(react_native_1.Animated.View, { style: {
                    marginVertical: RFValue_1.RSValue(10),
                    transform: [
                        {
                            translateX: animatedTrans,
                        },
                    ],
                    zIndex: 1,
                    position: 'absolute',
                    overflow: 'hidden',
                    borderRadius: RFValue_1.RSValue(20),
                } },
                react_1.default.createElement(react_native_1.Animated.View, { style: {
                        transform: [
                            {
                                translateX: animatedwidth,
                            },
                        ],
                        borderRadius: RFValue_1.RSValue(20),
                        backgroundColor: (_c = (_b = theme === null || theme === void 0 ? void 0 : theme.tabs) === null || _b === void 0 ? void 0 : _b.backgroundActiveColor) !== null && _c !== void 0 ? _c : 'black',
                        width: 8000,
                        height: RFValue_1.RSValue(30),
                    } })),
            react_1.default.createElement(react_native_1.View, { style: { flexDirection: 'row', zIndex: 10, paddingHorizontal: RFValue_1.RSValue(10) } }, options &&
                options.map(function (item, i) { return (react_1.default.createElement(Tab, { tabRef: layoutTabsRef, key: i, index: i, item: item, onPressTab: onPressTab, selected: i === selectTabIndex, skeleton: skeleton })); })))));
});
var Tab = react_1.default.memo(function Tab(_a) {
    var index = _a.index, item = _a.item, selected = _a.selected, onPressTab = _a.onPressTab, skeleton = _a.skeleton, tabRef = _a.tabRef;
    var layoutRef = react_1.useRef({});
    return (react_1.default.createElement(Tabs_style_1.TabContainer, { onLayout: function (ev) {
            layoutRef.current = ev.nativeEvent.layout;
            tabRef.current[index] = ev.nativeEvent.layout;
            if (selected) {
                onPressTab(item, index, layoutRef.current, false);
            }
        }, key: "tab-" + index, testID: "tab-" + index, selected: selected, onPress: function () { return !skeleton && onPressTab(item, index, layoutRef.current); } },
        !selected && react_1.default.createElement(Tabs_style_1.Tab, { skeleton: skeleton }, item.name),
        selected && react_1.default.createElement(Tabs_style_1.TabActive, { skeleton: skeleton }, item.name)));
});
//# sourceMappingURL=Tabs.js.map