"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styles_1 = require("@mui/material/styles");
const material_1 = require("@mui/material");
const system_1 = require("@mui/system");
const react_intl_1 = require("react-intl");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const PREFIX = 'SCCopyTextField';
const classes = {
    root: `${PREFIX}-root`,
    btnCopy: `${PREFIX}-btn-copy`
};
const Root = (0, styles_1.styled)(material_1.TextField, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({
    [`& .${classes.btnCopy}`]: {
        color: theme.palette.secondary.main
    }
}));
function CopyTextField(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className, label, onCopy, onChange } = props, rest = tslib_1.__rest(props, ["className", "label", "onCopy", "onChange"]);
    // STATE
    const [value, setValue] = (0, react_1.useState)('https://www.google.com');
    const [openConfirm, setOpenConfirm] = (0, react_1.useState)(false);
    // REF
    const timeout = (0, react_1.useRef)(null);
    // HANDLERS
    const handleChange = (event) => {
        setValue(event.target.value);
        onChange && onChange(event.target.value);
    };
    const handleCopy = (value) => {
        navigator.clipboard.writeText(value).then(function () {
            /* clipboard successfully set */
            hanldeCopySuccess();
        }, function () {
            /* clipboard write failed */
            console.log('Failed to opy text!');
        });
    };
    const hanldeCopySuccess = () => {
        setOpenConfirm(true);
        if (timeout) {
            clearTimeout(timeout.current);
            timeout.current = null;
        }
        timeout.current = setTimeout(() => {
            setOpenConfirm(false);
            onCopy && onCopy(value);
        }, 1500);
    };
    const button = ((0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ "aria-label": "Copy text", edge: "end", onClick: () => handleCopy(value), className: classes.btnCopy }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "insert_link" }) })));
    // RENDER
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className), label: label ? label : (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.shared.copyTextField.textToCopy", defaultMessage: "ui.shared.copyTextField.textToCopy" }), margin: "normal", variant: "outlined", disabled: true, fullWidth: true, value: value, multiline: true, onChange: handleChange, InputProps: {
            disableUnderline: true,
            endAdornment: ((0, jsx_runtime_1.jsx)(material_1.InputAdornment, Object.assign({ position: "end" }, { children: openConfirm ? ((0, jsx_runtime_1.jsx)(material_1.Tooltip, Object.assign({ open: openConfirm, PopperProps: { open: true }, title: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.shared.copyTextField.textCopied", defaultMessage: "ui.shared.copyTextField.textCopied" }) }, { children: button }))) : ((0, jsx_runtime_1.jsx)(material_1.Tooltip, Object.assign({ title: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.shared.copyTextField.textToCopy", defaultMessage: "ui.shared.copyTextField.textToCopy" }) }, { children: button }))) })))
        }, InputLabelProps: {
            shrink: true
        } }, rest)));
}
exports.default = CopyTextField;
