import React, { forwardRef, useLayoutEffect, useRef } from "react";
import { useForkRef } from "../../../../../hooks";
import { createEvent } from "../../../../../utils";
import { SelectHidden } from "./SelectNative.styles";
export var SelectNative = /*#__PURE__*/ forwardRef(function(param, ref) {
    var name = param.name, multiple = param.multiple, items = param.items, value = param.value, onChange = param.onChange, onSetValue = param.onSetValue;
    var values = multiple ? value : [
        value
    ];
    var selectRef = useRef(null);
    var forkRef = useForkRef(selectRef, ref);
    var options = Array.from(items.keys());
    useLayoutEffect(function() {
        if (selectRef.current && !multiple) {
            var valueInit = selectRef.current.value;
            if (valueInit) {
                onSetValue(valueInit);
            }
        }
        if (selectRef.current && multiple) {
            var valuesInit = Array.from(selectRef.current.selectedOptions).map(function(v) {
                return v.value;
            });
            if (valuesInit.length !== 0) {
                onSetValue(valuesInit);
            }
        }
    }, []);
    useLayoutEffect(function() {
        var event = createEvent(selectRef);
        if (onChange) {
            onChange(event);
        }
    }, [
        value
    ]);
    return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(SelectHidden, {
        ref: forkRef,
        multiple: multiple,
        name: name,
        hidden: true,
        value: multiple ? values : values[0]
    }, /*#__PURE__*/ React.createElement("option", null, " "), options.map(function(v) {
        return /*#__PURE__*/ React.createElement("option", {
            key: v,
            value: v
        }, v);
    })));
});
