"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = PHXTargetSelect;
const tslib_1 = require("tslib");
const react_1 = require("react");
const query_1 = require("./query");
const getLoginInfo_1 = tslib_1.__importDefault(require("../Func/getLoginInfo"));
const PHXGrpcClientV3_1 = require("../Func/GRPC/PHXGrpcClientV3");
const Select_1 = require("../Select");
const react_2 = tslib_1.__importDefault(require("react"));
const constants_1 = require("../../utils/constants");
const react_fontawesome_1 = require("@fortawesome/react-fontawesome");
const free_solid_svg_icons_1 = require("@fortawesome/free-solid-svg-icons");
function PHXTargetSelect({ defaultId, label = 'Đối tượng hiển thị', onChange, disabled = false, register, errorMessageCustom, errorType, error, className = '', helpText = '', openManagePageAction = false, }) {
    const [loading, setLoading] = (0, react_1.useState)(true);
    const userInfo = (0, getLoginInfo_1.default)();
    const [options, setOptions] = (0, react_1.useState)([]);
    const getSelectOption = async () => {
        try {
            const { data: { user_segment_target }, } = await (0, PHXGrpcClientV3_1.PHXClientQueryV3)({
                query: query_1.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: constants_1.NOT_FOUND_ID, name: '' };
        onChange === null || onChange === void 0 ? void 0 : onChange(selectedOption);
    };
    (0, react_1.useEffect)(() => {
        getSelectOption();
    }, []);
    return (react_2.default.createElement(react_2.default.Fragment, null, loading ? null : (react_2.default.createElement(Select_1.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_2.default.createElement("div", { className: 'flex items-center gap-x-1' },
                    react_2.default.createElement("p", { className: 'text-link font-medium' }, "Qu\u1EA3n l\u00FD"),
                    react_2.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: free_solid_svg_icons_1.faArrowUpRightFromSquare }))),
            }
            : {} },
        react_2.default.createElement("option", { value: '' }, "L\u1EF1a ch\u1ECDn"),
        options.map((option) => (react_2.default.createElement("option", { key: option.id, value: option.id }, option.name)))))));
}
//# sourceMappingURL=target-select.js.map