import { useEffect, useState } from 'react';
import { getListSelectTargetQuery } from './query';
import PHXFuncGetLoggedInfo from '../Func/getLoginInfo';
import { PHXClientQueryV3 } from '../Func/GRPC/PHXGrpcClientV3';
import { PHXSelect } from '../Select';
import React from 'react';
import { NOT_FOUND_ID } from '../../utils/constants';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faArrowUpRightFromSquare } from '@fortawesome/free-solid-svg-icons';
export default function PHXTargetSelect({ defaultId, label = 'Đối tượng hiển thị', onChange, disabled = false, register, errorMessageCustom, errorType, error, className = '', helpText = '', openManagePageAction = false, }) {
    const [loading, setLoading] = useState(true);
    const userInfo = PHXFuncGetLoggedInfo();
    const [options, setOptions] = useState([]);
    const getSelectOption = async () => {
        try {
            const { data: { user_segment_target }, } = await PHXClientQueryV3({
                query: getListSelectTargetQuery,
                variables: {
                    schoolId: userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id,
                },
            });
            setOptions(user_segment_target);
        }
        catch (e) {
            console.error(e);
        }
        finally {
            setLoading(false);
        }
    };
    const handleSelect = (selectedId) => {
        // lựa chọn ko tồn tại trong list sẽ trả về id = -1
        const selectedOption = options.find((option) => option.id === selectedId) || { id: NOT_FOUND_ID, name: '' };
        onChange === null || onChange === void 0 ? void 0 : onChange(selectedOption);
    };
    useEffect(() => {
        getSelectOption();
    }, []);
    return (React.createElement(React.Fragment, null, loading ? null : (React.createElement(PHXSelect, { helpText: helpText, className: className, disabled: disabled, defaultValue: defaultId, label: label, ...(register ? { register } : { onChange: (e) => handleSelect(e.target.value) }), error: error, errorType: errorType, errorMessageCustom: errorMessageCustom, 
        // @ts-ignore
        labelAction: openManagePageAction
            ? {
                id: 'open_manage',
                onClick: () => window.open('/user-segment/manage', '_blank'),
                content: (React.createElement("div", { className: 'flex items-center gap-x-1' },
                    React.createElement("p", { className: 'text-link font-medium' }, "Qu\u1EA3n l\u00FD"),
                    React.createElement(FontAwesomeIcon, { icon: faArrowUpRightFromSquare }))),
            }
            : {} },
        React.createElement("option", { value: '' }, "L\u1EF1a ch\u1ECDn"),
        options.map((option) => (React.createElement("option", { key: option.id, value: option.id }, option.name)))))));
}
//# sourceMappingURL=target-select.js.map