import { useState, useCallback } from 'react';
import { Direction } from '../constants';
import { getOppositeDirection, getSingleDirectionSelectedKeysExcludeDisabled, } from '../utils';
export default function useTransfer(params) {
    var _a = params || {}, _b = _a.targetKeys, defaultTargetKeys = _b === void 0 ? [] : _b, _c = _a.selectedKeys, defaultSelectedKeys = _c === void 0 ? [] : _c, _d = _a.disabledKeys, disabledKeys = _d === void 0 ? [] : _d;
    var _e = useState(defaultTargetKeys), targetKeys = _e[0], setTargetKeys = _e[1];
    var _f = useState(defaultSelectedKeys), selectedKeys = _f[0], setSelectedKeys = _f[1];
    var getSingleDirectionSelectedKeys = useCallback(function (direction) {
        return selectedKeys.filter(function (key) {
            return Direction.Left === direction
                ? !targetKeys.includes(key)
                : targetKeys.includes(key);
        });
    }, [selectedKeys, targetKeys]);
    var transferKeys = useCallback(function (direction) {
        var transferredKeys = getSingleDirectionSelectedKeysExcludeDisabled({
            direction: getOppositeDirection(direction),
            selectedKeys: selectedKeys,
            targetKeys: targetKeys,
            disabledKeys: disabledKeys,
        });
        setSelectedKeys(selectedKeys.filter(function (item) { return !transferredKeys.includes(item); }));
        setTargetKeys(Direction.Right === direction
            ? transferredKeys.concat(targetKeys)
            : targetKeys.filter(function (item) { return !transferredKeys.includes(item); }));
    }, [selectedKeys, targetKeys, disabledKeys]);
    var changeSelectedKeys = useCallback(function (direction, keys) {
        setSelectedKeys(keys.concat(getSingleDirectionSelectedKeys(getOppositeDirection(direction))));
    }, [getSingleDirectionSelectedKeys]);
    var resetSelectedKeys = useCallback(function (keys) {
        setSelectedKeys(keys);
    }, []);
    var resetTargetKeys = useCallback(function (keys) {
        setTargetKeys(keys);
    }, []);
    return {
        targetKeys: targetKeys,
        selectedKeys: selectedKeys,
        transferKeys: transferKeys,
        changeSelectedKeys: changeSelectedKeys,
        resetSelectedKeys: resetSelectedKeys,
        resetTargetKeys: resetTargetKeys,
    };
}
