function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React from 'react';
import { locales } from '@pisell/utils';
import CompensatedPisellContainer from "../../../pro/compensatedPisellContainer";
import useInitLocalesOnce from "../../../hooks/useInitLocalesOnce";
import { shouldSkipSalesSdkSaveConfirm, writeSalesSdkSkipSaveConfirm } from "../utils/saveConfirmPreferences";
import "./SaveConfirmModal.less";
var SAVE_CONFIRM_LOCALES = {
  en: {
    'pisell2.sales-sdk.save-confirm.title': 'Confirm save',
    'pisell2.sales-sdk.save-confirm.desc': 'Are you sure you want to save the changes?',
    'pisell2.sales-sdk.save-confirm.dont-remind': "Don't remind me again",
    'pisell2.sales-sdk.save-confirm.cancel': 'Cancel',
    'pisell2.sales-sdk.save-confirm.save': 'Save'
  },
  'zh-CN': {
    'pisell2.sales-sdk.save-confirm.title': '确认保存',
    'pisell2.sales-sdk.save-confirm.desc': '确定要保存当前更改吗？',
    'pisell2.sales-sdk.save-confirm.dont-remind': '不再提醒',
    'pisell2.sales-sdk.save-confirm.cancel': '取消',
    'pisell2.sales-sdk.save-confirm.save': '保存'
  },
  'zh-HK': {
    'pisell2.sales-sdk.save-confirm.title': '確認保存',
    'pisell2.sales-sdk.save-confirm.desc': '確定要保存目前更改嗎？',
    'pisell2.sales-sdk.save-confirm.dont-remind': '不再提醒',
    'pisell2.sales-sdk.save-confirm.cancel': '取消',
    'pisell2.sales-sdk.save-confirm.save': '保存'
  },
  ja: {
    'pisell2.sales-sdk.save-confirm.title': '保存の確認',
    'pisell2.sales-sdk.save-confirm.desc': '現在の変更を保存しますか？',
    'pisell2.sales-sdk.save-confirm.dont-remind': '今後表示しない',
    'pisell2.sales-sdk.save-confirm.cancel': 'キャンセル',
    'pisell2.sales-sdk.save-confirm.save': '保存'
  },
  pt: {
    'pisell2.sales-sdk.save-confirm.title': 'Confirmar salvamento',
    'pisell2.sales-sdk.save-confirm.desc': 'Tem certeza de que deseja salvar as alterações?',
    'pisell2.sales-sdk.save-confirm.dont-remind': 'Não me lembrar novamente',
    'pisell2.sales-sdk.save-confirm.cancel': 'Cancelar',
    'pisell2.sales-sdk.save-confirm.save': 'Salvar'
  }
};
/**
 * SalesSdk 通用保存二次确认弹窗；加时保存与 BigSale 保存共用同一“不再提醒”偏好。
 */
export var SaveConfirmModal = function SaveConfirmModal(_ref) {
  var open = _ref.open,
    onCancel = _ref.onCancel,
    onConfirm = _ref.onConfirm;
  useInitLocalesOnce(SAVE_CONFIRM_LOCALES);
  var _React$useState = React.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    dontRemind = _React$useState2[0],
    setDontRemind = _React$useState2[1];
  React.useEffect(function () {
    if (open) return;
    setDontRemind(false);
  }, [open]);

  /** 确认保存时先持久化通用偏好，再交给业务入口执行原保存流程。 */
  var handleConfirm = React.useCallback(function () {
    if (dontRemind) {
      writeSalesSdkSkipSaveConfirm(true);
    }
    onConfirm();
  }, [dontRemind, onConfirm]);
  return /*#__PURE__*/React.createElement(CompensatedPisellContainer, {
    renderType: "modal",
    props: {
      open: open,
      closable: true,
      width: 520,
      centered: true,
      destroyOnClose: true,
      maskClosable: false,
      zIndex: 2000,
      title: locales.getText('pisell2.sales-sdk.save-confirm.title'),
      className: 'pisell-sales-sdk-save-confirm-modal',
      headerDivider: false,
      onCancel: onCancel,
      footer: /*#__PURE__*/React.createElement("div", {
        className: "pisell-sales-sdk-save-confirm__footer"
      }, /*#__PURE__*/React.createElement("button", {
        type: "button",
        className: "pisell-sales-sdk-save-confirm__btn",
        onClick: onCancel
      }, locales.getText('pisell2.sales-sdk.save-confirm.cancel')), /*#__PURE__*/React.createElement("button", {
        type: "button",
        className: "pisell-sales-sdk-save-confirm__btn pisell-sales-sdk-save-confirm__btn--primary",
        onClick: handleConfirm
      }, locales.getText('pisell2.sales-sdk.save-confirm.save')))
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sales-sdk-save-confirm"
  }, /*#__PURE__*/React.createElement("p", null, locales.getText('pisell2.sales-sdk.save-confirm.desc')), /*#__PURE__*/React.createElement("label", {
    className: "pisell-sales-sdk-save-confirm__checkbox"
  }, /*#__PURE__*/React.createElement("input", {
    type: "checkbox",
    checked: dontRemind,
    onChange: function onChange(event) {
      return setDontRemind(event.target.checked);
    }
  }), /*#__PURE__*/React.createElement("span", null, locales.getText('pisell2.sales-sdk.save-confirm.dont-remind')))));
};

/** 保存入口统一先走这里，确保所有保存功能共享同一跳过确认偏好。 */
export var shouldOpenSaveConfirmModal = function shouldOpenSaveConfirmModal() {
  return !shouldSkipSalesSdkSaveConfirm();
};