import { __assign, __awaiter, __generator } from "tslib";
import { useState } from 'functional-mini/compat';
import { useDidMount, useEvent, useRef, } from 'functional-mini/component';
import '../_util/assert-component2';
import { mountComponent } from '../_util/component';
import { useComponentEvent } from '../_util/hooks/useComponentEvent';
import { useEvent as useStableCallback } from '../_util/hooks/useEvent';
import { useInstanceBoundingClientRect } from '../_util/hooks/useInstanceBoundingClientRect';
import { useComponentUpdateEffect } from '../_util/hooks/useLayoutEffect';
import { useMixState } from '../_util/hooks/useMixState';
import { TabsFunctionalProps } from './props';
var Tabs = function (props) {
    var _a = useMixState(props.defaultCurrent, {
        value: props.current,
    }), currentValue = _a[0], _b = _a[1], isControlled = _b.isControlled, update = _b.update;
    var _c = useState({
        scrollLeft: 0,
        scrollTop: 0,
        leftFade: false,
        rightFade: false,
    }), state = _c[0], updateState = _c[1];
    var scrollRef = useRef({ scrollLeft: 0, scrollTop: 0 });
    var triggerEvent = useComponentEvent(props).triggerEvent;
    var updatePartState = function (part) {
        updateState(function (old) {
            return __assign(__assign({}, old), part);
        });
    };
    var _d = useInstanceBoundingClientRect(), getBoundingClientRectWithBuilder = _d.getBoundingClientRectWithBuilder, getBoundingClientRect = _d.getBoundingClientRect;
    var updateFade = useStableCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
        var _a, view, item;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    updatePartState({
                        leftFade: !!scrollRef.current.scrollLeft,
                    });
                    return [4 /*yield*/, Promise.all([
                            getBoundingClientRectWithBuilder(function (id) { return "#ant-tabs-bar-scroll-view".concat(id); }),
                            getBoundingClientRectWithBuilder(function (id) { return "#ant-tabs-bar-item".concat(id, "-").concat(props.items.length - 1); }),
                        ])];
                case 1:
                    _a = _b.sent(), view = _a[0], item = _a[1];
                    if (!item || !view) {
                        return [2 /*return*/];
                    }
                    updatePartState({
                        rightFade: item.left + item.width / 2 > view.width,
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    var updateScroll = useStableCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
        var current, _a, view, item, scrollTop, needScroll_1, distance, scrollLeft, needScroll, distance;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    current = currentValue;
                    return [4 /*yield*/, Promise.all([
                            getBoundingClientRectWithBuilder(function (id) { return "#ant-tabs-bar-scroll-view".concat(id); }),
                            getBoundingClientRectWithBuilder(function (id) { return "#ant-tabs-bar-item".concat(id, "-").concat(current); }),
                        ])];
                case 1:
                    _a = _b.sent(), view = _a[0], item = _a[1];
                    if (!view || !item) {
                        return [2 /*return*/];
                    }
                    if (props.direction === 'vertical') {
                        scrollTop = scrollRef.current.scrollTop || 0;
                        needScroll_1 = false;
                        if (props.scrollMode === 'center') {
                            needScroll_1 = true;
                            scrollTop +=
                                item.top - view.top - Math.max((view.height - item.height) / 2, 0);
                        }
                        else {
                            distance = item.top - view.top;
                            if (distance < 0) {
                                scrollTop += distance;
                                needScroll_1 = true;
                            }
                            else if (distance + item.height > view.height) {
                                scrollTop += Math.min(distance + item.height - view.height, distance);
                                needScroll_1 = true;
                            }
                        }
                        if (needScroll_1) {
                            if (scrollTop === state.scrollTop) {
                                scrollTop += Math.random();
                            }
                            updatePartState({
                                scrollTop: scrollTop,
                            });
                        }
                        return [2 /*return*/];
                    }
                    scrollLeft = scrollRef.current.scrollLeft || 0;
                    needScroll = false;
                    if (props.scrollMode === 'center') {
                        needScroll = true;
                        scrollLeft +=
                            item.left - view.left - Math.max((view.width - item.width) / 2, 0);
                    }
                    else {
                        distance = item.left - view.left;
                        if (distance < 0) {
                            scrollLeft += distance;
                            needScroll = true;
                        }
                        else if (distance + item.width > view.width) {
                            scrollLeft += Math.min(distance + item.width - view.width, distance);
                            needScroll = true;
                        }
                    }
                    if (needScroll) {
                        if (scrollLeft === state.scrollLeft) {
                            scrollLeft += Math.random();
                        }
                        updatePartState({
                            scrollLeft: scrollLeft,
                        });
                        updateFade();
                    }
                    return [2 /*return*/];
            }
        });
    }); });
    useEvent('onScroll', function (e) { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            if (props.direction === 'vertical') {
                scrollRef.current.scrollTop = e.detail.scrollTop;
                return [2 /*return*/];
            }
            scrollRef.current.scrollLeft = e.detail.scrollLeft;
            updateFade();
            return [2 /*return*/];
        });
    }); });
    useEvent('onChange', function (e) { return __awaiter(void 0, void 0, void 0, function () {
        var index;
        return __generator(this, function (_a) {
            index = parseInt(e.currentTarget.dataset.index, 10);
            if (props.items[index].disabled) {
                return [2 /*return*/];
            }
            if (currentValue === index) {
                return [2 /*return*/];
            }
            if (!isControlled) {
                update(index);
            }
            triggerEvent('change', index, e);
            return [2 /*return*/];
        });
    }); });
    useDidMount(function () {
        updateScroll();
    }, []);
    useComponentUpdateEffect(function () {
        updateScroll();
    }, [props.items, currentValue]);
    return __assign({ mixin: {
            value: currentValue,
        } }, state);
};
mountComponent(Tabs, TabsFunctionalProps);