import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useImperativeHandle, forwardRef, useState } from "react";
import { View } from "@tarojs/components";
import { ArrowLeft } from "@nutui/icons-react-taro";
import Popup from "../popup/index";
import { ExistRender } from "./existRender";
import { ElevatorRender } from "./elevatorRender";
import { CascaderRender } from "./cascaderRender";
import { useConfig } from "../configprovider/index";
import { ComponentDefaults } from "../../utils/typings";
import { usePropsValue } from "../../hooks/use-props-value";
import { mergeProps } from "../../utils/merge-props";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    defaultValue: [],
    type: 'cascader',
    options: [],
    optionKey: {
        textKey: 'text',
        valueKey: 'value',
        childrenKey: 'children'
    },
    format: {},
    custom: false,
    existList: [],
    hotList: [],
    height: '200px',
    defaultIcon: null,
    selectIcon: null,
    closeIcon: null,
    backIcon: null
});
var InternalAddress = function(props, ref) {
    var locale = useConfig().locale;
    var _mergeProps = mergeProps(defaultProps, props), style = _mergeProps.style, className = _mergeProps.className, visible = _mergeProps.visible, defaultVisible = _mergeProps.defaultVisible, defaultValue = _mergeProps.defaultValue, children = _mergeProps.children, type = _mergeProps.type, options = _mergeProps.options, optionKey = _mergeProps.optionKey, format = _mergeProps.format, height = _mergeProps.height, title = _mergeProps.title, existList = _mergeProps.existList, custom = _mergeProps.custom, selectIcon = _mergeProps.selectIcon, defaultIcon = _mergeProps.defaultIcon, closeIcon = _mergeProps.closeIcon, backIcon = _mergeProps.backIcon, hotList = _mergeProps.hotList, onChange = _mergeProps.onChange, onExistSelect = _mergeProps.onExistSelect, onClose = _mergeProps.onClose, onSwitch = _mergeProps.onSwitch, rest = _object_without_properties(_mergeProps, [
        "style",
        "className",
        "visible",
        "defaultVisible",
        "defaultValue",
        "children",
        "type",
        "options",
        "optionKey",
        "format",
        "height",
        "title",
        "existList",
        "custom",
        "selectIcon",
        "defaultIcon",
        "closeIcon",
        "backIcon",
        "hotList",
        "onChange",
        "onExistSelect",
        "onClose",
        "onSwitch"
    ]);
    var classPrefix = 'nut-address';
    var _useState = _sliced_to_array(useState(type), 2), currentType = _useState[0], setCurrentType = _useState[1];
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: visible,
        defaultValue: defaultVisible,
        finalValue: defaultVisible
    }), 2), innerVisible = _usePropsValue[0], setInnerVisible = _usePropsValue[1];
    useImperativeHandle(ref, function() {
        return {
            open: function open() {
                setInnerVisible(true);
            },
            close: function close() {
                setInnerVisible(false);
            }
        };
    });
    var handleClose = function() {
        setInnerVisible(false);
        onClose && onClose();
    };
    var renderLeftOnCascaderSwitch = function() {
        if (!custom) return null;
        return /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-left-icon"),
            onClick: onSwitchModule
        }, /*#__PURE__*/ React.isValidElement(backIcon) ? backIcon : /*#__PURE__*/ React.createElement(ArrowLeft, {
            color: "#cccccc"
        }));
    };
    var selectedExistItem = function(data) {
        onExistSelect && onExistSelect(data);
        handleClose();
    };
    var onSwitchModule = function() {
        var nextType = currentType === 'exist' ? 'cascader' : 'exist';
        setCurrentType(nextType);
        onSwitch && onSwitch({
            type: nextType
        });
    };
    var renderElevator = function() {
        return /*#__PURE__*/ React.createElement(ElevatorRender, {
            visible: innerVisible,
            closeable: true,
            title: title || locale.address.selectRegion,
            left: backIcon,
            defaultValue: defaultValue,
            closeIcon: closeIcon,
            options: options,
            hotList: hotList,
            format: format,
            optionKey: optionKey,
            type: currentType,
            height: height,
            onClose: handleClose,
            onChange: function(val, params) {
                onChange === null || onChange === void 0 ? void 0 : onChange(val, params);
            }
        });
    };
    var renderCascator = function() {
        return /*#__PURE__*/ React.createElement(CascaderRender, {
            visible: innerVisible,
            closeable: true,
            title: title || locale.address.selectRegion,
            left: renderLeftOnCascaderSwitch(),
            defaultValue: defaultValue,
            closeIcon: closeIcon,
            options: options,
            format: format,
            optionKey: optionKey,
            type: currentType,
            height: height,
            onClose: handleClose,
            onChange: function(val, params) {
                onChange && onChange(val, params);
            }
        });
    };
    var renderExist = function() {
        return /*#__PURE__*/ React.createElement(Popup, {
            visible: innerVisible,
            position: "bottom",
            round: true,
            closeable: true,
            closeIcon: closeIcon,
            title: title || locale.address.selectRegion,
            onClose: handleClose
        }, /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, " ").concat(className || ''),
            style: _object_spread({}, style)
        }, // 不需要 close，选中切换即关闭弹框。可手动关闭弹框，只关闭弹框不处理逻辑。
        /*#__PURE__*/ React.createElement(ExistRender, {
            type: currentType,
            existList: existList,
            selectIcon: selectIcon,
            defaultIcon: defaultIcon,
            custom: custom,
            onSelect: selectedExistItem,
            onSwitch: onSwitchModule
        })));
    };
    return /*#__PURE__*/ React.createElement(React.Fragment, null, currentType === 'elevator' ? renderElevator() : null, currentType === 'cascader' ? renderCascator() : null, currentType === 'exist' ? renderExist() : null);
};
export var Address = /*#__PURE__*/ forwardRef(InternalAddress);
Address.displayName = 'NutAddress';
