"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_native_1 = require("react-native");
const styled_components_1 = require("styled-components");
const components_1 = require("../../themes/components");
const borderRadius_1 = __importDefault(require("../../themes/foundations/borderRadius"));
const OptionMenu = (props) => {
    const inputRef = react_1.useRef();
    const [open, setOpen] = react_1.useState(false);
    const { colors } = react_1.useContext(styled_components_1.ThemeContext);
    const handleClick = (index) => {
        const { options } = props;
        for (let i = 0; i < options.length; i += 1) {
            if (index === i) {
                if (index === options.length - 1) {
                    const open = false;
                    setOpen(open);
                }
                else if (props.actions[i] !== null) {
                    props.actions[i]();
                }
            }
        }
    };
    const handlePress = () => {
        const { options } = props;
        if (react_native_1.Platform.OS === 'ios') {
            let destructiveIndex = -1;
            if (Number.isInteger(props.destructiveIndex) &&
                props.destructiveIndex >= 0) {
                destructiveIndex = props.destructiveIndex;
            }
            react_native_1.ActionSheetIOS.showActionSheetWithOptions({
                options,
                destructiveButtonIndex: destructiveIndex,
                cancelButtonIndex: options.length - 1,
            }, (buttonIndex) => {
                handleClick(buttonIndex);
            });
        }
        else if (react_native_1.Platform.OS === 'android') {
            react_native_1.UIManager.showPopupMenu(react_native_1.findNodeHandle(inputRef.current), options, () => console.log('something went wrong with the popup menu'), (e, i) => {
                handleClick(i);
            });
        }
    };
    const { options } = props;
    const optionsUI = open ? (react_1.default.createElement(react_native_1.View, { style: {
            position: 'absolute',
            bottom: '100%',
            right: '50%',
            flex: 1,
            elevation: 3,
            shadowColor: 'black',
            shadowOpacity: 0.3,
            shadowOffset: { width: 0, height: 2 },
            shadowRadius: 4,
            borderRadius: borderRadius_1.default.small,
            backgroundColor: 'white',
        } }, options.map((option, index) => (react_1.default.createElement(react_native_1.View, { key: option },
        react_1.default.createElement(react_native_1.TouchableOpacity, { style: { padding: 10 }, onPress: () => handleClick(index) },
            react_1.default.createElement(components_1.Text, { color: "text.100", align: "center" }, option)),
        index < props.options.length - 1 && (react_1.default.createElement(react_native_1.View, { style: {
                flex: 1,
                height: 1,
                backgroundColor: 'lightgray',
            } }))))))) : null;
    return (react_1.default.createElement(react_native_1.View, null,
        react_1.default.createElement(react_native_1.View, null,
            react_1.default.createElement(react_native_1.TouchableOpacity, { ref: inputRef, onPress: handlePress },
                react_1.default.createElement(components_1.Icon, { boxSize: "xl", ml: 5, name: "dots-vertical", fill: colors.textPrimary[300] }))),
        optionsUI));
};
exports.default = OptionMenu;
//# sourceMappingURL=index.js.map