import { PickerView as DoubaoPickerView, PickerColumn as DoubaoPickerColumn } from '@byted-doubao-apps/components';
import { warnUnsupported } from '../base/warn-unsupported.js';
import { buildOptionsFromChildren, buildOptionsFromRange, mapPickerViewColumnProps, mapPickerViewProps, resolveExplicitItemHeight, resolveItemHeightFromChildren, resolveSelectedIndexValue, toPickerViewChangeEvent } from './map.js';
const PICKER_VIEW_COLUMN_TYPE = 'taro-picker-view-column';
export function PickerView(props) {
    const { value, defaultValue, onChange, children, itemHeight: propItemHeight, ...rest } = props;
    const { mapped, unsupported, indicatorItemHeight } = mapPickerViewProps(rest);
    const explicitItemHeight = resolveExplicitItemHeight(propItemHeight);
    const pickerViewId = typeof mapped.id === 'string' ? mapped.id : '';
    warnUnsupported('PickerView', propItemHeight !== undefined && explicitItemHeight === undefined
        ? [...unsupported, 'itemHeight must be a positive number']
        : unsupported);
    const columns = [];
    toChildArray(children).forEach((child) => {
        if (!isPickerViewColumnElement(child)) {
            return;
        }
        const columnProps = child.props;
        const { className, style, unsupported: columnUnsupported } = mapPickerViewColumnProps(columnProps);
        warnUnsupported('PickerViewColumn', columnUnsupported);
        columns.push({
            options: Array.isArray(columnProps.range)
                ? buildOptionsFromRange(columnProps.range)
                : buildOptionsFromChildren(columnProps.children),
            className,
            style,
            itemHeight: resolveItemHeightFromChildren(columnProps.children)
        });
    });
    const resolvedItemHeight = explicitItemHeight ??
        indicatorItemHeight ??
        columns.find((column) => column.itemHeight !== undefined)?.itemHeight;
    const handleChange = (values, selectedOptions) => {
        const indices = values.map((selectedValue, index) => {
            if (typeof selectedValue === 'number') {
                return selectedValue;
            }
            const optionValue = selectedOptions[index]?.value;
            return typeof optionValue === 'number' ? optionValue : 0;
        });
        onChange?.(toPickerViewChangeEvent(indices, pickerViewId));
    };
    return (<DoubaoPickerView {...mapped} itemHeight={resolvedItemHeight} onChange={onChange ? handleChange : undefined}>
      {columns.map(({ options, className, style }, index) => {
            const valueForColumn = resolveSelectedIndexValue(value, index, options);
            const defaultValueForColumn = resolveSelectedIndexValue(defaultValue, index, options);
            return (<DoubaoPickerColumn key={index} options={options} value={valueForColumn} defaultValue={defaultValueForColumn} className={className} style={style}/>);
        })}
    </DoubaoPickerView>);
}
export function PickerViewColumn(_props) {
    // This is a placeholder wrapper. Real rendering is performed by PickerView above after mapping.
    return null;
}
PickerViewColumn.__TARO_PICKER_VIEW_CHILD_TYPE = PICKER_VIEW_COLUMN_TYPE;
function isPickerViewColumnElement(child) {
    if (!isReactElement(child)) {
        return false;
    }
    const childType = child.type;
    return (childType.__TARO_PICKER_VIEW_CHILD_TYPE === PICKER_VIEW_COLUMN_TYPE ||
        childType.displayName === 'PickerViewColumn' ||
        childType.name === 'PickerViewColumn' ||
        child.type === 'picker-view-column' ||
        child.type === 'taro-picker-view-column-core');
}
function isReactElement(node) {
    return (typeof node === 'object' &&
        node !== null &&
        node.$$typeof === Symbol.for('react.element') &&
        'type' in node &&
        'props' in node);
}
function toChildArray(children) {
    if (children === null || children === undefined || typeof children === 'boolean') {
        return [];
    }
    return Array.isArray(children) ? children.flatMap(toChildArray) : [children];
}
//# sourceMappingURL=index.jsx.map