import React, { useEffect } from 'react';
import { PisellViewGrid, Skeleton } from '@pisell/materials';
import { isMobile, locales } from '@pisell/utils';
import { useResource, useSales, useCart, useStep } from "../../../../Sales";
import ResourceDisplay from "../../resourceDisplay";
import { resourceTitle } from "../../../utils";
import "./index.less";
var Resource = function Resource() {
  var _data$resourceData2, _data$resourceData3, _data$resourceData4;
  var _useSales = useSales(),
    data = _useSales.data;
  var _useResource = useResource(),
    getResourceList = _useResource.getResourceList,
    selectResource = _useResource.selectResource;
  var _useCart = useCart(),
    createCartItem = _useCart.createCartItem;
  var _useStep = useStep(),
    nextStep = _useStep.nextStep;
  useEffect(function () {
    var _data$resourceData, _resourceList$, _data$stepData;
    var resourceList = (data === null || data === void 0 || (_data$resourceData = data.resourceData) === null || _data$resourceData === void 0 ? void 0 : _data$resourceData.resourceList) || [];
    if ((resourceList === null || resourceList === void 0 ? void 0 : resourceList.length) <= 0) {
      return;
    }

    // 如果只有一个资源，则直接选中并进入下一步
    var resourceListChildren = (resourceList === null || resourceList === void 0 || (_resourceList$ = resourceList[0]) === null || _resourceList$ === void 0 ? void 0 : _resourceList$.children) || [];
    if ((resourceList === null || resourceList === void 0 ? void 0 : resourceList.length) === 1 && (resourceListChildren === null || resourceListChildren === void 0 ? void 0 : resourceListChildren.length) === 1 && data !== null && data !== void 0 && (_data$stepData = data.stepData) !== null && _data$stepData !== void 0 && _data$stepData.isAutoOperate) {
      createCartItem();
      selectResource(resourceListChildren[0]);
      nextStep();
    }
  }, [data === null || data === void 0 || (_data$resourceData2 = data.resourceData) === null || _data$resourceData2 === void 0 ? void 0 : _data$resourceData2.resourceList]);

  /**
   * @title: 选择资源
   * @description:
   * @param {any} item
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-15 18:08
   */
  var handleClick = function handleClick(item) {
    createCartItem();
    selectResource(item);
    nextStep();
  };

  /**
   * @title: 渲染内容
   * @description:
   * @param {any} item
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-15 11:30
   */
  var renderItem = function renderItem(item) {
    return /*#__PURE__*/React.createElement(ResourceDisplay, {
      mode: "campus",
      dataSource: item,
      direction: "vertical",
      shape: "square",
      bordered: false,
      borderRadius: 8,
      padding: 8,
      onClick: function onClick() {
        return handleClick(item);
      }
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "event-interface-resource-wrap ".concat(isMobile() ? 'event-interface-resource-mobile-wrap' : '')
  }, /*#__PURE__*/React.createElement("span", {
    className: "title"
  }, locales.getText('pisell2.event.select.campus')(resourceTitle(data))), /*#__PURE__*/React.createElement("div", {
    className: "content"
  }, !(data !== null && data !== void 0 && (_data$resourceData3 = data.resourceData) !== null && _data$resourceData3 !== void 0 && (_data$resourceData3 = _data$resourceData3.resourceList) !== null && _data$resourceData3 !== void 0 && _data$resourceData3.length) ? /*#__PURE__*/React.createElement(Skeleton, {
    loading: true,
    active: true,
    paragraph: {
      rows: 3
    }
  }) : data === null || data === void 0 || (_data$resourceData4 = data.resourceData) === null || _data$resourceData4 === void 0 || (_data$resourceData4 = _data$resourceData4.resourceList) === null || _data$resourceData4 === void 0 ? void 0 : _data$resourceData4.map(function (item, index) {
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      className: "content-item"
    }, /*#__PURE__*/React.createElement("span", {
      className: "content-title"
    }, item === null || item === void 0 ? void 0 : item.title), /*#__PURE__*/React.createElement(PisellViewGrid, {
      dataSource: item === null || item === void 0 ? void 0 : item.children,
      columns: isMobile() ? 1 : 3,
      verticalGap: 20,
      horizontalGap: 20,
      loading: false,
      renderItem: renderItem,
      className: "content-grid"
    }));
  })));
};
export default Resource;