var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/schedules/resources/index.tsx
var resources_exports = {};
__export(resources_exports, {
  default: () => resources_default
});
module.exports = __toCommonJS(resources_exports);
var import_ahooks = require("ahooks");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_CurrentDay = __toESM(require("../components/CurrentDay"));
var import_Search = __toESM(require("../components/Search"));
var import_model = require("../model");
var import_add = __toESM(require("./add"));
var import_edit = __toESM(require("./edit"));
var import_detail = __toESM(require("./detail"));
var import_index = require("./index.less");
var import_list = __toESM(require("./list"));
var import_serve = require("./serve");
var import_utils = require("@pisell/utils");
var Resources = () => {
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const { currentSelectDayDetail, resources, tabKey } = state;
  const cacheResource = resources == null ? void 0 : resources.cacheResource;
  const resourcesAddRef = (0, import_react.useRef)();
  const resourcesEditRef = (0, import_react.useRef)();
  const resourcesDetailRef = (0, import_react.useRef)();
  const [seachText, setSearchText] = (0, import_react.useState)("");
  const [activeKey, setActiveKey] = (0, import_react.useState)([]);
  const [formTypes, setFormTypes] = (0, import_react.useState)([]);
  const [relationResource, setRelationResource] = (0, import_react.useState)([]);
  const searchChange = (e) => {
    setSearchText(e);
  };
  const _getFormTypes = (0, import_ahooks.useRequest)(import_serve.getFormTypes, {
    manual: true,
    onSuccess: (result) => {
      var _a;
      const list = ((_a = result == null ? void 0 : result.data) == null ? void 0 : _a.list) || [];
      setFormTypes(list);
      setActiveKey(list.map((item) => item.id));
    }
  });
  const _getRelationResource = (0, import_ahooks.useRequest)(import_serve.getRelationResource, {
    manual: true,
    onSuccess: (result, params) => {
      var _a;
      const list = ((_a = result == null ? void 0 : result.data) == null ? void 0 : _a.list) || [];
      setRelationResource(list);
    }
  });
  const init = (params) => {
    _getRelationResource.run(
      {
        skip: 1,
        num: 500,
        schedule_ids: currentSelectDayDetail ? currentSelectDayDetail.scheduleIds || [] : void 0,
        exclude_form_code: ["opening_hours"]
      },
      params || null
    );
  };
  (0, import_react.useEffect)(() => {
    _getFormTypes.run({
      type: "manual",
      num: 500,
      skip: 1
    });
  }, []);
  (0, import_react.useEffect)(() => {
    init();
  }, [currentSelectDayDetail == null ? void 0 : currentSelectDayDetail.scheduleIds]);
  (0, import_react.useEffect)(() => {
    var _a, _b;
    if (cacheResource) {
      (_b = (_a = resourcesEditRef.current) == null ? void 0 : _a.open) == null ? void 0 : _b.call(_a, cacheResource);
      setTimeout(() => {
        var _a2;
        const el = document.getElementById(`${cacheResource.id}`);
        console.log("el", el);
        (_a2 = el == null ? void 0 : el.scrollIntoView) == null ? void 0 : _a2.call(el, true);
      }, 100);
    }
  }, [cacheResource]);
  const items = (0, import_react.useMemo)(() => {
    const _relationResource = relationResource.filter((item) => {
      var _a, _b;
      return (_b = (_a = item.title) == null ? void 0 : _a.includes) == null ? void 0 : _b.call(_a, seachText);
    });
    let _allData = formTypes.reduce((arr, item) => {
      const currentResource = _relationResource.filter(
        (relation) => relation.form_id === item.id
      );
      if (currentResource.length > 0) {
        arr.push({
          label: item.title,
          key: item.id,
          children: currentResource,
          ...item
        });
      }
      return arr;
    }, []);
    return _allData.map((item) => {
      const resources2 = item.children || [];
      return {
        label: item.title,
        key: item.id,
        children: /* @__PURE__ */ import_react.default.createElement(
          import_list.default,
          {
            list: resources2.map((resource) => {
              return {
                id: resource.id,
                title: resource.title,
                form_id: item.id
              };
            }),
            addClick: () => {
              var _a, _b;
              (_b = (_a = resourcesAddRef.current) == null ? void 0 : _a.open) == null ? void 0 : _b.call(_a, item.id);
            },
            itemClick: (resource) => {
              var _a, _b;
              (_b = (_a = resourcesEditRef.current) == null ? void 0 : _a.open) == null ? void 0 : _b.call(_a, resource);
            }
          }
        )
      };
    });
  }, [formTypes, seachText, relationResource]);
  const handleClose = () => {
    dispatch({
      type: "setState",
      payload: {
        currentSelectDayDetail: null
      }
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__schedules-component-resources" }, /* @__PURE__ */ import_react.default.createElement("div", { style: { padding: "0 32px" } }, /* @__PURE__ */ import_react.default.createElement(
    import_CurrentDay.default,
    {
      onClose: handleClose,
      currentDay: currentSelectDayDetail == null ? void 0 : currentSelectDayDetail.date
    }
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "resources-header" }, /* @__PURE__ */ import_react.default.createElement(import_Search.default, { value: seachText, onChange: searchChange })), /* @__PURE__ */ import_react.default.createElement("div", { className: "resources-list" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Skeleton,
    {
      loading: _getRelationResource.loading,
      style: { marginTop: 12 }
    },
    items.length ? /* @__PURE__ */ import_react.default.createElement(
      import_antd.Collapse,
      {
        activeKey,
        ghost: true,
        items,
        expandIconPosition: "end",
        onChange: (key) => {
          setActiveKey(key);
        }
      }
    ) : /* @__PURE__ */ import_react.default.createElement(
      import_antd.Empty,
      {
        image: import_antd.Empty.PRESENTED_IMAGE_SIMPLE,
        description: import_utils.locales.getText(
          "pisell2.text.products.no-resources"
        )
      }
    )
  ))), /* @__PURE__ */ import_react.default.createElement(import_add.default, { ref: resourcesAddRef, onSuccess: init }), /* @__PURE__ */ import_react.default.createElement(import_edit.default, { ref: resourcesEditRef, onSuccess: init }), /* @__PURE__ */ import_react.default.createElement(
    import_detail.default,
    {
      tabKey,
      ref: resourcesDetailRef,
      onSuccess: (e) => {
        if (e) {
          init();
        }
      }
    }
  ));
};
var resources_default = Resources;
