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 { ArrowLeft } from "@nutui/icons-react";
import Popup from "../popup";
import { CustomRender } from "./customRender";
import { ExistRender } from "./existRender";
import { useConfig } from "../configprovider";
import { ComponentDefaults } from "../../utils/typings";
import { usePropsValue } from "../../hooks/use-props-value";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    defaultValue: [],
    type: 'custom',
    options: [],
    optionKey: {
        textKey: 'text',
        valueKey: 'value',
        childrenKey: 'children'
    },
    format: {},
    custom: false,
    existList: [],
    height: '200px',
    defaultIcon: null,
    selectIcon: null,
    closeIcon: null,
    backIcon: null
});
export var InternalAddress = function(props, ref) {
    var locale = useConfig().locale;
    var _ref = _object_spread({}, defaultProps, props), style = _ref.style, className = _ref.className, visible = _ref.visible, defaultVisible = _ref.defaultVisible, defaultValue = _ref.defaultValue, children = _ref.children, type = _ref.type, options = _ref.options, optionKey = _ref.optionKey, format = _ref.format, height = _ref.height, title = _ref.title, existList = _ref.existList, custom = _ref.custom, selectIcon = _ref.selectIcon, defaultIcon = _ref.defaultIcon, closeIcon = _ref.closeIcon, backIcon = _ref.backIcon, onChange = _ref.onChange, onExistSelect = _ref.onExistSelect, onClose = _ref.onClose, onSwitch = _ref.onSwitch, rest = _object_without_properties(_ref, [
        "style",
        "className",
        "visible",
        "defaultVisible",
        "defaultValue",
        "children",
        "type",
        "options",
        "optionKey",
        "format",
        "height",
        "title",
        "existList",
        "custom",
        "selectIcon",
        "defaultIcon",
        "closeIcon",
        "backIcon",
        "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 renderLeftOnCustomSwitch = function() {
        if (custom) {
            return /*#__PURE__*/ React.createElement("div", {
                className: "".concat(classPrefix, "-left-icon"),
                onClick: onSwitchModule
            }, /*#__PURE__*/ React.isValidElement(backIcon) ? backIcon : /*#__PURE__*/ React.createElement(ArrowLeft, {
                color: "#cccccc"
            }));
        }
        return null;
    };
    var selectedExistItem = function(data) {
        onExistSelect && onExistSelect(data);
        handleClose();
    };
    var onSwitchModule = function() {
        if (currentType === 'exist') {
            setCurrentType('custom');
        } else {
            setCurrentType('exist');
        }
        onSwitch && onSwitch({
            type: currentType
        });
    };
    return /*#__PURE__*/ React.createElement(React.Fragment, null, currentType === 'custom' || currentType === 'custom2' ? /*#__PURE__*/ React.createElement(CustomRender, {
        visible: innerVisible,
        closeable: true,
        title: title || locale.address.selectRegion,
        left: renderLeftOnCustomSwitch(),
        defaultValue: defaultValue,
        closeIcon: closeIcon,
        options: options,
        format: format,
        optionKey: optionKey,
        type: currentType,
        height: height,
        onClose: handleClose,
        onChange: function(val, params) {
            onChange === null || onChange === void 0 ? void 0 : onChange(val, params);
        }
    }) : /*#__PURE__*/ React.createElement(Popup, {
        visible: innerVisible,
        position: "bottom",
        round: true,
        closeable: true,
        closeIcon: closeIcon,
        title: title || locale.address.selectRegion,
        onClose: handleClose
    }, /*#__PURE__*/ React.createElement("div", {
        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
    }))));
};
export var Address = /*#__PURE__*/ forwardRef(InternalAddress);
Address.displayName = 'NutAddress';
