"use strict";

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.for-each.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/es.object.define-properties.js");
require("core-js/modules/es.object.define-property.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.object.get-own-property-descriptors.js");
require("core-js/modules/es.object.keys.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.filter.js");
require("core-js/modules/esnext.iterator.for-each.js");
require("core-js/modules/web.dom-collections.for-each.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ConfirmContent = void 0;
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.reverse.js");
require("core-js/modules/es.number.is-finite.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.parse-float.js");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = require("react");
var _icon = require("../icon");
var _button = require("../button");
var _type = require("../icon/type");
var _checkbox = require("../checkbox");
require("./index.css");
var _i18n = require("../../i18n");
var _jsxRuntime = require("react/jsx-runtime");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var ConfirmContent = exports.ConfirmContent = function ConfirmContent(props) {
  var t = (0, _i18n.useI18n)();
  var onOk = props.onOk,
    onCancel = props.onCancel,
    onClose = props.onClose,
    _props$closable = props.closable,
    closable = _props$closable === void 0 ? true : _props$closable,
    closeIcon = props.closeIcon,
    _props$cancelText = props.cancelText,
    cancelText = _props$cancelText === void 0 ? t('fmt_popup_button_cancel') : _props$cancelText,
    _props$okText = props.okText,
    okText = _props$okText === void 0 ? t('fmt_uimanager_button_determine') : _props$okText,
    _props$showCancel = props.showCancel,
    showCancel = _props$showCancel === void 0 ? true : _props$showCancel,
    _props$showOk = props.showOk,
    showOk = _props$showOk === void 0 ? true : _props$showOk,
    width = props.width,
    title = props.title,
    content = props.content,
    icon = props.icon,
    alignCenter = props.alignCenter,
    alignLeft = props.alignLeft,
    cancelButtonProps = props.cancelButtonProps,
    okButtonProps = props.okButtonProps,
    reverseButton = props.reverseButton,
    tip = props.tip,
    className = props.className,
    checkboxOption = props.checkboxOption;
  var wrapperRef = (0, _react.useRef)(null);
  var contentBodyRef = (0, _react.useRef)(null);
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    checkboxChecked = _useState2[0],
    setCheckboxChecked = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
    isCenterContentMultiline = _useState4[0],
    setIsCenterContentMultiline = _useState4[1];
  var defaultColor = 'var(--fcr_ui_scene_icontext1)';
  var hoverColor = 'var(--fcr_ui_scene_ramp_brand6)';
  var _useState5 = (0, _react.useState)(defaultColor),
    _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
    color = _useState6[0],
    setColor = _useState6[1];
  var handleHover = function handleHover() {
    setColor(hoverColor);
  };
  var handleLeave = function handleLeave() {
    setColor(defaultColor);
  };
  (0, _react.useEffect)(function () {
    if (wrapperRef.current && width) {
      wrapperRef.current.style.setProperty('width', width + 'px');
    }
  }, [width]);
  var handleOk = function handleOk() {
    if (checkboxOption) {
      onOk === null || onOk === void 0 || onOk({
        checkboxChecked: checkboxChecked
      });
    } else {
      onOk === null || onOk === void 0 || onOk();
    }
  };
  var buttons = [showCancel && /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, _objectSpread(_objectSpread({
    size: "XS",
    type: "secondary",
    onClick: onCancel
  }, cancelButtonProps), {}, {
    children: cancelText
  }), 0), showOk && /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, _objectSpread(_objectSpread({
    size: "XS",
    onClick: handleOk
  }, okButtonProps), {}, {
    children: okText
  }), 1)];
  var layoutType = (0, _react.useMemo)(function () {
    if (alignLeft) return 'left';
    return alignCenter ? 'center' : 'space-between';
  }, [alignCenter, alignLeft]);
  var centerContentWithoutIcon = layoutType === 'center' && !icon;
  (0, _react.useLayoutEffect)(function () {
    if (typeof content !== 'string') {
      setIsCenterContentMultiline(false);
      return;
    }
    if (!centerContentWithoutIcon) {
      setIsCenterContentMultiline(false);
      return;
    }
    var el = contentBodyRef.current;
    if (!el) {
      setIsCenterContentMultiline(false);
      return;
    }
    var measure = function measure() {
      var lhRaw = getComputedStyle(el).lineHeight;
      var parsed = parseFloat(lhRaw);
      var lineHeight = Number.isFinite(parsed) ? parsed : 19.5;
      var wrapped = el.scrollHeight > lineHeight + 2;
      setIsCenterContentMultiline(wrapped);
    };
    measure();
    var ro = new ResizeObserver(measure);
    ro.observe(el);
    return function () {
      return ro.disconnect();
    };
  }, [centerContentWithoutIcon, content, width]);
  var isCenter = layoutType === 'center' && buttons.filter(function (btn) {
    return btn;
  }).length === 1;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    ref: wrapperRef,
    className: (0, _classnames["default"])('fcr-confirm-content-wrapper', className, {
      'fcr-confirm-content-wrapper__center': layoutType === 'center',
      'fcr-confirm-content-wrapper__align-left': layoutType === 'left'
    }),
    children: [closable ? closeIcon ? closeIcon : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      onClick: onClose || onCancel,
      className: (0, _classnames["default"])('fcr-confirm-content-dialog-close'),
      onMouseEnter: handleHover,
      onMouseLeave: handleLeave,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
        type: _type.FcrIconType.FCR_CLOSE,
        colors: {
          iconPrimary: color
        },
        size: 14
      })
    }) : null, /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: (0, _classnames["default"])("fcr-confirm-container", {
        'align-center': layoutType === 'center' && !icon,
        'align-left': layoutType === 'left',
        icon: icon
      }),
      children: [icon && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-confirm-content-icon",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
          type: icon,
          size: 40
        })
      }), title && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-confirm-content-title",
        children: title
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: (0, _classnames["default"])('fcr-confirm-content-content', {
          'fcr-confirm-content-content--center-multiline': isCenterContentMultiline
        }),
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          ref: contentBodyRef,
          className: "fcr-confirm-content-body",
          children: content
        }), checkboxOption && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr-confirm-content-checkbox",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_checkbox.FcrCheckbox, {
            checked: checkboxChecked,
            onChange: setCheckboxChecked,
            label: checkboxOption.label,
            size: "small"
          })
        }), tip && /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
          className: "fcr-confirm-content-tip fcr-confirm-content-tip-".concat(tip.type || 'error'),
          children: tip.message
        })]
      })]
    }), (showCancel || showOk) && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames["default"])('fcr-confirm-content-footer', {
        'fcr-button-flex-column': (cancelButtonProps === null || cancelButtonProps === void 0 ? void 0 : cancelButtonProps.block) || (okButtonProps === null || okButtonProps === void 0 ? void 0 : okButtonProps.block),
        'fcr-confirm-content-footer__center': layoutType === 'center',
        'fcr-confirm-content-footer__center-button': isCenter,
        'fcr-confirm-content-footer__left': layoutType === 'left'
      }),
      children: reverseButton ? buttons.reverse() : buttons
    })]
  });
};