function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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; }
import React, { useCallback, useMemo } from 'react';
import classNames from 'classnames';
import "./BucketButtonGroup.less";

/**
 * 通用「桶 → 底层 key 数组」按钮组，HUD 抽屉里的支付状态 / 预约状态快筛都走这个组件。
 *
 * 设计动机：
 *   - 业务方在抽屉里只关心少量"业务桶"（如未支付 / 已支付 / 在场），但底层枚举更细
 *     （`partially_paid` / `payment_processing` / `arrived` / `started` …）；
 *   - 直接渲染所有底层 key 会有 7+ 个按钮把抽屉撑爆，且语义不连贯；
 *   - 桶 → 底层 key 多对一映射在前端组件里完成，写入 `searchParams.filter.values.<field>`
 *     仍是底层 key 数组，与 Filter modal 的 multi-select / 后端查询参数完全兼容。
 *
 * "选中"判定：桶 keys 任一在 value 中即视为选中（容忍历史/边角态 key）。
 * "切换"行为：选中 → 剔除该桶所有 key；未选中 → 加入该桶所有 key（保留 value 中已有非桶 key）。
 */

function isBucketChecked(bucket, valueSet) {
  return bucket.keys.some(function (k) {
    return valueSet.has(k);
  });
}
function toggleBucket(bucket, current) {
  var set = new Set(current.map(String));
  var checked = isBucketChecked(bucket, set);
  if (checked) {
    var _iterator = _createForOfIteratorHelper(bucket.keys),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var k = _step.value;
        set.delete(k);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  } else {
    var _iterator2 = _createForOfIteratorHelper(bucket.keys),
      _step2;
    try {
      for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
        var _k = _step2.value;
        set.add(_k);
      }
    } catch (err) {
      _iterator2.e(err);
    } finally {
      _iterator2.f();
    }
  }
  return Array.from(set);
}
export function BucketButtonGroup(props) {
  var value = props.value,
    onChange = props.onChange,
    buckets = props.buckets;
  var valueSet = useMemo(function () {
    return new Set(Array.isArray(value) ? value.map(String) : []);
  }, [value]);
  var handleToggle = useCallback(function (bucket) {
    onChange === null || onChange === void 0 || onChange(toggleBucket(bucket, Array.isArray(value) ? value : []));
  }, [value, onChange]);
  return /*#__PURE__*/React.createElement("div", {
    className: "reservation-bucket-button-group"
  }, buckets.map(function (bucket) {
    var checked = isBucketChecked(bucket, valueSet);
    return /*#__PURE__*/React.createElement("button", {
      type: "button",
      key: bucket.id,
      title: bucket.label,
      className: classNames('reservation-bucket-button-group__item', {
        'reservation-bucket-button-group__item--checked': checked
      }),
      onClick: function onClick() {
        return handleToggle(bucket);
      },
      "aria-pressed": checked
    }, /*#__PURE__*/React.createElement("span", {
      className: "reservation-bucket-button-group__dot",
      "aria-hidden": "true"
    }), /*#__PURE__*/React.createElement("span", {
      className: "reservation-bucket-button-group__label"
    }, bucket.label));
  }));
}
export default BucketButtonGroup;