function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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) { _defineProperty(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; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
/**
 * venueBooking 步骤导航头部
 * 展示三步流程进度条（选场地 → 附加商品 → 结账），支持点击跳转已完成步骤
 */
import React, { useMemo, useRef, useState } from 'react';
import { Modal } from 'antd';
import { locales } from '@pisell/utils';
import { Iconfont } from '@pisell/materials';
import { useVenueBookingContext } from "../../context";
export default function Header() {
  var _viewModel$addonProdu2;
  var _useVenueBookingConte = useVenueBookingContext(),
    currentStep = _useVenueBookingConte.currentStep,
    canNext = _useVenueBookingConte.canNext,
    viewModel = _useVenueBookingConte.viewModel,
    goToStep = _useVenueBookingConte.goToStep,
    prevStep = _useVenueBookingConte.prevStep;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    popupOpen = _useState2[0],
    setPopupOpen = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    confirmOpen = _useState4[0],
    setConfirmOpen = _useState4[1];
  var cardRef = useRef(null);

  /**
   * 步骤配置：当 addonProductList 为空时跳过 addons 步骤，自动重排序号。
   * - 有附加商品：select-venue → addons → checkout（3 步）
   * - 无附加商品：select-venue → checkout（2 步）
   */
  var STEP_ITEMS = useMemo(function () {
    var _viewModel$addonProdu;
    var hasAddons = (((_viewModel$addonProdu = viewModel.addonProductList) === null || _viewModel$addonProdu === void 0 ? void 0 : _viewModel$addonProdu.length) || 0) > 0;
    var base = [{
      key: 'select-venue',
      titleKey: 'pisell2.venue.step.select'
    }].concat(_toConsumableArray(hasAddons ? [{
      key: 'addons',
      titleKey: 'pisell2.venue.step.addons'
    }] : []), [{
      key: 'checkout',
      titleKey: 'pisell2.venue.step.checkout'
    }]);
    return base.map(function (item, idx) {
      return _objectSpread(_objectSpread({}, item), {}, {
        n: idx + 1
      });
    });
  }, [(_viewModel$addonProdu2 = viewModel.addonProductList) === null || _viewModel$addonProdu2 === void 0 ? void 0 : _viewModel$addonProdu2.length]);
  var currentStepIndex = STEP_ITEMS.findIndex(function (item) {
    return item.key === currentStep;
  });
  var currentStepItem = STEP_ITEMS[currentStepIndex] || STEP_ITEMS[0];
  /** 是否处于第一步:第一步时返回箭头视为退出预订 */
  var isFirstStep = currentStepIndex <= 0;

  /** 返回上一步:首步时弹出离开确认,后续步骤直接回退 */
  function handleBack() {
    if (isFirstStep) {
      setConfirmOpen(true);
      return;
    }
    prevStep();
  }

  /** 判断某步骤是否可点击跳转 */
  function isClickable(targetStep) {
    if (targetStep.key === 'select-venue') return true;
    if (targetStep.key === 'addons') return canNext;
    return false;
  }

  /** 获取步骤状态：done / active / pending */
  function getStepStatus(item) {
    var itemIndex = STEP_ITEMS.findIndex(function (s) {
      return s.key === item.key;
    });
    if (itemIndex < currentStepIndex) return 'done';
    if (itemIndex === currentStepIndex) return 'active';
    return 'pending';
  }

  /** 步骤 1 完成时的对勾徽章 */
  function getBadge(item) {
    var status = getStepStatus(item);
    if (item.key === 'select-venue' && canNext && status === 'done') {
      return /*#__PURE__*/React.createElement("span", {
        className: "ml-1 inline-flex h-5 w-5 items-center justify-center rounded-full text-[11px] font-bold text-white",
        style: {
          backgroundColor: 'var(--vb-primary)'
        }
      }, /*#__PURE__*/React.createElement(Iconfont, {
        type: "pisell2-check",
        style: {
          fontSize: 12
        }
      }));
    }
    if (item.key === 'addons') {
      // 只统计附加商品（无 start_date/end_date），排除场地预订商品
      var addonCount = (viewModel.cartItems || []).filter(function (c) {
        return !(c !== null && c !== void 0 && c.start_date) || !(c !== null && c !== void 0 && c.end_date);
      }).length;
      if (addonCount <= 0) return null;
      return /*#__PURE__*/React.createElement("span", {
        className: "ml-1 inline-flex min-w-[20px] items-center justify-center rounded-full bg-[#111111] px-1.5 text-[11px] font-bold text-white"
      }, "+", addonCount);
    }
    return null;
  }

  /** 返回上一页：优先回退历史，无历史时回到首页 */
  function doBackHome() {
    if (typeof window === 'undefined') return;
    if (window.history.length > 1) {
      window.history.back();
      return;
    }
    window.location.href = '/';
  }

  /** 可返回的步骤（当前步骤之前且允许跳转） */
  var backSteps = STEP_ITEMS.filter(function (item) {
    var idx = STEP_ITEMS.findIndex(function (s) {
      return s.key === item.key;
    });
    return idx < currentStepIndex && isClickable(item);
  });
  return /*#__PURE__*/React.createElement("div", {
    ref: cardRef,
    className: "relative bg-white p-3 md:rounded-[24px] md:p-4 ring-1 ring-black/6 shadow-[0_10px_30px_rgba(20,20,20,0.04)]"
  }, /*#__PURE__*/React.createElement(Modal, {
    open: confirmOpen,
    onCancel: function onCancel() {
      return setConfirmOpen(false);
    },
    title: String(locales.getText('pisell2.venue.leave.title') || 'Leave booking?'),
    centered: true,
    footer: /*#__PURE__*/React.createElement("div", {
      style: {
        display: 'flex',
        gap: 24
      }
    }, /*#__PURE__*/React.createElement("button", {
      type: "button",
      onClick: function onClick() {
        return setConfirmOpen(false);
      },
      style: {
        flex: 1,
        borderRadius: 12,
        border: 'none',
        background: '#f4f4f5',
        padding: '12px 0',
        fontSize: 16,
        fontWeight: 600,
        color: '#3f3f46',
        cursor: 'pointer'
      }
    }, String(locales.getText('pisell2.venue.leave.stay') || 'Stay')), /*#__PURE__*/React.createElement("button", {
      type: "button",
      onClick: function onClick() {
        setConfirmOpen(false);
        doBackHome();
      },
      style: {
        flex: 1,
        borderRadius: 12,
        border: 'none',
        background: 'var(--vb-primary, #ea1018)',
        padding: '12px 0',
        fontSize: 16,
        fontWeight: 700,
        color: '#fff',
        cursor: 'pointer'
      }
    }, String(locales.getText('pisell2.venue.leave.confirm') || 'Leave')))
  }, /*#__PURE__*/React.createElement("p", {
    className: "text-sm text-zinc-500"
  }, String(locales.getText('pisell2.venue.leave.desc') || 'Your selections will be lost if you leave now.'))), popupOpen && backSteps.length > 0 ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "fixed inset-0 z-40 md:hidden",
    onClick: function onClick() {
      return setPopupOpen(false);
    }
  }), /*#__PURE__*/React.createElement("div", {
    className: "absolute left-4 right-4 top-full z-50 mt-2 overflow-hidden rounded-2xl bg-white shadow-[0_8px_30px_rgba(20,20,20,0.12)] ring-1 ring-black/6 md:hidden"
  }, backSteps.map(function (item, idx) {
    return /*#__PURE__*/React.createElement("button", {
      key: item.key,
      type: "button",
      onClick: function onClick() {
        goToStep(item.key);
        setPopupOpen(false);
      },
      className: "flex w-full items-center gap-3 px-4 py-3 text-left transition hover:bg-zinc-50 active:bg-zinc-100 ".concat(idx > 0 ? 'border-t border-zinc-100' : '')
    }, /*#__PURE__*/React.createElement("div", {
      className: "flex h-8 min-w-[62px] shrink-0 items-center justify-center rounded-full bg-zinc-100 px-2.5 text-xs font-bold text-zinc-700"
    }, item.n, "/", STEP_ITEMS.length), /*#__PURE__*/React.createElement("div", {
      className: "flex items-center text-sm font-semibold text-zinc-800"
    }, /*#__PURE__*/React.createElement("span", null, locales.getText(item.titleKey) || '-'), getBadge(item)));
  }))) : null, /*#__PURE__*/React.createElement("div", {
    className: "flex items-center gap-3"
  }, !isFirstStep ? /*#__PURE__*/React.createElement("button", {
    type: "button",
    "aria-label": "Back",
    onClick: handleBack,
    className: "flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-zinc-50 text-zinc-700 ring-1 ring-black/10 transition hover:bg-zinc-100"
  }, /*#__PURE__*/React.createElement("svg", {
    viewBox: "0 0 24 24",
    className: "h-5 w-5",
    fill: "none",
    stroke: "currentColor",
    strokeWidth: "2.5",
    strokeLinecap: "round",
    strokeLinejoin: "round"
  }, /*#__PURE__*/React.createElement("path", {
    d: "M15 18l-6-6 6-6"
  }))) : null, /*#__PURE__*/React.createElement("div", {
    className: "min-w-0 flex-1"
  }, /*#__PURE__*/React.createElement("div", {
    className: "md:hidden ".concat(backSteps.length > 0 ? 'cursor-pointer' : ''),
    onClick: function onClick() {
      if (backSteps.length > 0) setPopupOpen(function (v) {
        return !v;
      });
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "flex items-center gap-3"
  }, /*#__PURE__*/React.createElement("div", {
    className: "flex h-8 min-w-[62px] items-center justify-center rounded-full px-2.5 text-xs font-bold md:h-10 md:min-w-[74px] md:text-sm",
    style: {
      backgroundColor: 'var(--vb-primary-light)',
      color: 'var(--vb-primary)'
    }
  }, currentStepIndex + 1, "/", STEP_ITEMS.length), /*#__PURE__*/React.createElement("div", {
    className: "min-w-0 flex-1 text-lg font-extrabold text-zinc-900 md:text-xl"
  }, locales.getText(currentStepItem.titleKey) || '-'), backSteps.length > 0 ? /*#__PURE__*/React.createElement("svg", {
    viewBox: "0 0 24 24",
    className: "h-5 w-5 shrink-0 text-zinc-400 transition-transform ".concat(popupOpen ? 'rotate-180' : ''),
    fill: "none",
    stroke: "currentColor",
    strokeWidth: "2"
  }, /*#__PURE__*/React.createElement("path", {
    d: "M6 9l6 6 6-6",
    strokeLinecap: "round",
    strokeLinejoin: "round"
  })) : null)), /*#__PURE__*/React.createElement("div", {
    className: "hidden items-center gap-3 md:flex"
  }, STEP_ITEMS.map(function (item, index) {
    var status = getStepStatus(item);
    var clickable = isClickable(item);
    return /*#__PURE__*/React.createElement(React.Fragment, {
      key: item.n
    }, /*#__PURE__*/React.createElement("button", {
      type: "button",
      onClick: function onClick() {
        if (clickable) goToStep(item.key);
      },
      className: "flex items-center gap-3 rounded-full px-3 py-2 transition ".concat(status === 'done' ? 'bg-zinc-50' : status !== 'active' ? 'bg-zinc-50/80' : '', " ").concat(clickable ? 'cursor-pointer hover:bg-zinc-100' : 'cursor-default'),
      style: status === 'active' ? {
        backgroundColor: 'var(--vb-primary-light)'
      } : undefined
    }, /*#__PURE__*/React.createElement("div", {
      className: "flex h-8 w-8 items-center justify-center rounded-full text-sm font-bold ".concat(status === 'done' ? 'bg-[#111] text-white' : status === 'active' ? 'text-white' : 'bg-white text-zinc-400 ring-1 ring-black/8'),
      style: status === 'active' ? {
        backgroundColor: 'var(--vb-primary)'
      } : undefined
    }, status === 'done' ? /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-check",
      style: {
        fontSize: 15
      }
    }) : item.n), /*#__PURE__*/React.createElement("div", {
      className: "flex items-center text-sm font-semibold ".concat(status === 'active' ? 'text-zinc-900' : status === 'done' ? 'text-zinc-700' : 'text-zinc-400')
    }, /*#__PURE__*/React.createElement("span", null, locales.getText(item.titleKey) || '-'), getBadge(item))), index < STEP_ITEMS.length - 1 ? /*#__PURE__*/React.createElement("div", {
      className: "relative h-px flex-1 overflow-hidden rounded-full bg-zinc-200"
    }, /*#__PURE__*/React.createElement("div", {
      className: "absolute left-0 top-0 h-full ".concat(status === 'done' ? 'w-full bg-[#111]' : status === 'active' ? 'w-1/2' : 'w-0'),
      style: status === 'active' ? {
        backgroundColor: 'var(--vb-primary)'
      } : undefined
    })) : null);
  }))), /*#__PURE__*/React.createElement("button", {
    type: "button",
    "aria-label": "Close",
    onClick: setConfirmOpen.bind(null, true),
    className: "flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-zinc-50 text-zinc-700 ring-1 ring-black/10 transition hover:bg-zinc-100"
  }, /*#__PURE__*/React.createElement("svg", {
    viewBox: "0 0 24 24",
    className: "h-5 w-5",
    fill: "none",
    stroke: "currentColor",
    strokeWidth: "2.5",
    strokeLinecap: "round",
    strokeLinejoin: "round"
  }, /*#__PURE__*/React.createElement("path", {
    d: "M18 6 6 18"
  }), /*#__PURE__*/React.createElement("path", {
    d: "M6 6l12 12"
  })))));
}