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 _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 _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 _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, { useEffect, forwardRef, useImperativeHandle, useRef } from 'react';
import { locales } from '@pisell/utils';
import localeTexts from "./locales";
import stockLocaleTexts from "../../shared/locales/stock";
import { useLanguage, useStore, useSymbol } from "./hooks";
import Selector from "../../pro/Selector";
import { transformBundleGroup, transformOptionGroup, transformVariantGroup, transformSelectorValueToValue, transformValueToSelectorValue } from "./utils";
import { store, actions } from "./store";
import "./index.less";
var SelectorGroup = Selector.Group;
var SKUOptionsSelection = /*#__PURE__*/forwardRef(function (props, ref) {
  var language = useLanguage();
  var symbol = useSymbol();
  locales.init(localeTexts, language);
  locales.init(stockLocaleTexts, language);
  var selectorGroupRef = useRef(null);
  var selectorDataSource = useStore(function (s) {
    return s.selectorDataSource;
  });
  var selectorValue = useStore(function (s) {
    return s.selectorValue;
  });
  useImperativeHandle(ref, function () {
    return {
      validate: function validate() {
        var _selectorGroupRef$cur;
        return (_selectorGroupRef$cur = selectorGroupRef.current) === null || _selectorGroupRef$cur === void 0 ? void 0 : _selectorGroupRef$cur.validate();
      },
      getValues: function getValues() {
        var _selectorGroupRef$cur2;
        return (_selectorGroupRef$cur2 = selectorGroupRef.current) === null || _selectorGroupRef$cur2 === void 0 ? void 0 : _selectorGroupRef$cur2.getValues();
      },
      getTransformValues: function getTransformValues() {
        var _values = store.getState().selectorValue;
        return transformSelectorValueToValue(_values);
      }
    };
  });

  // 处理符号
  useEffect(function () {
    actions.updateSymbol(symbol);
  }, [symbol]);

  // 处理配置
  useEffect(function () {
    actions.updateConfig((props === null || props === void 0 ? void 0 : props.config) || {});
  }, [props === null || props === void 0 ? void 0 : props.config]);

  // 处理 skuCard 配置
  useEffect(function () {
    if (props !== null && props !== void 0 && props.skuCardConfig) {
      actions.updateSkuCardConfig(props.skuCardConfig);
    }
  }, [props === null || props === void 0 ? void 0 : props.skuCardConfig]);

  // 处理自定义配置
  useEffect(function () {
    actions.updateCustomConfig((props === null || props === void 0 ? void 0 : props.customConfig) || {});
  }, [props === null || props === void 0 ? void 0 : props.customConfig]);
  useEffect(function () {
    actions.updateDataSource((props === null || props === void 0 ? void 0 : props.dataSource) || {});
  }, [props === null || props === void 0 ? void 0 : props.dataSource]);

  // 处理原始值
  useEffect(function () {
    actions.updateValue((props === null || props === void 0 ? void 0 : props.value) || {});
  }, [props === null || props === void 0 ? void 0 : props.value]);
  useEffect(function () {
    var _transformValues = transformValueToSelectorValue((props === null || props === void 0 ? void 0 : props.value) || null, (props === null || props === void 0 ? void 0 : props.dataSource) || null);
    actions.updateSelectorValue(_transformValues);
  }, [props === null || props === void 0 ? void 0 : props.value, props === null || props === void 0 ? void 0 : props.dataSource]);
  useEffect(function () {
    var _props$dataSource, _props$dataSource3, _props$dataSource5;
    var _groupDataSource = [];
    // 处理组合规格组数据
    if (props !== null && props !== void 0 && (_props$dataSource = props.dataSource) !== null && _props$dataSource !== void 0 && _props$dataSource.variant_group) {
      var _props$dataSource2;
      var renderVariantGroup = transformVariantGroup(props === null || props === void 0 || (_props$dataSource2 = props.dataSource) === null || _props$dataSource2 === void 0 ? void 0 : _props$dataSource2.variant_group);
      _groupDataSource.push.apply(_groupDataSource, _toConsumableArray(renderVariantGroup));
    }

    // 处理单规格组数据
    if (props !== null && props !== void 0 && (_props$dataSource3 = props.dataSource) !== null && _props$dataSource3 !== void 0 && _props$dataSource3.option_group) {
      var _props$dataSource4;
      var renderOptionGroup = transformOptionGroup(props === null || props === void 0 || (_props$dataSource4 = props.dataSource) === null || _props$dataSource4 === void 0 ? void 0 : _props$dataSource4.option_group);
      _groupDataSource.push.apply(_groupDataSource, _toConsumableArray(renderOptionGroup));
    }

    // 处理套餐商品组数据
    if (props !== null && props !== void 0 && (_props$dataSource5 = props.dataSource) !== null && _props$dataSource5 !== void 0 && _props$dataSource5.bundle_group) {
      var _props$dataSource6;
      var renderBundleGroup = transformBundleGroup(props === null || props === void 0 || (_props$dataSource6 = props.dataSource) === null || _props$dataSource6 === void 0 ? void 0 : _props$dataSource6.bundle_group);
      _groupDataSource.push.apply(_groupDataSource, _toConsumableArray(renderBundleGroup));
    }
    actions.updateSelectorDataSource(_groupDataSource);
  }, [props === null || props === void 0 ? void 0 : props.dataSource]);
  useEffect(function () {
    return function () {
      var _selectorGroupRef$cur3, _selectorGroupRef$cur4;
      // 清空选择
      (_selectorGroupRef$cur3 = selectorGroupRef.current) === null || _selectorGroupRef$cur3 === void 0 || (_selectorGroupRef$cur4 = _selectorGroupRef$cur3.clear) === null || _selectorGroupRef$cur4 === void 0 || _selectorGroupRef$cur4.call(_selectorGroupRef$cur3);
      // 清空整个store的值
      actions.clearStore();
    };
  }, []);
  return /*#__PURE__*/React.createElement(SelectorGroup, {
    ref: selectorGroupRef,
    dataSource: selectorDataSource,
    value: selectorValue,
    onChange: function onChange(values) {
      var _props$onChange;
      console.log('selector_onChange_values>>>>>>>', values);
      actions.updateSelectorValue(values);
      var transformValue = transformSelectorValueToValue(values);
      console.log('selector_onChange_transformValue>>>>>>>', transformValue);
      props === null || props === void 0 || (_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, transformValue);
    },
    style: props === null || props === void 0 ? void 0 : props.style,
    customScrollParent: props === null || props === void 0 ? void 0 : props.customScrollParent
  });
});
export default SKUOptionsSelection;