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/booking/materiels/resources/index.tsx
var resources_exports = {};
__export(resources_exports, {
  default: () => resources_default
});
module.exports = __toCommonJS(resources_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_AlertTriangle = __toESM(require("@pisell/icon/es/AlertTriangle"));
var import_mock = require("../../mock");
var import_utils = require("../../utils");
var import_utils2 = require("@pisell/utils");
var Resources = (0, import_react.forwardRef)((props, ref) => {
  const {
    locale = "en",
    resources = [],
    watch,
    getResources,
    resourcesChange,
    currentDate,
    cacheItem,
    selectProps,
    ...resetProps
  } = props;
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [_resources, set_resources] = (0, import_react.useState)([]);
  const renderOptions = (0, import_react.useCallback)(
    (array, value = "value", label = "label", type) => {
      return array.map((d) => {
        let props2 = {};
        if (type === "resource") {
          props2.labelText = d.labelText;
        }
        return /* @__PURE__ */ import_react.default.createElement(import_antd.Select.Option, { value: d[value], key: d[value], ...props2 }, d[label]);
      });
    },
    []
  );
  (0, import_react.useEffect)(() => {
    _getResources();
  }, [watch]);
  const _getResources = async () => {
    setLoading(true);
    let data;
    if (getResources) {
      data = await getResources({
        skip: 1,
        num: 1e3,
        with_date_times: currentDate,
        codes: ["resources", "therapist", "table"]
      });
    } else {
      data = resources;
    }
    let _list = [(0, import_mock.getNoResource)()].concat((data == null ? void 0 : data.list) || []);
    const _cacheItem = (0, import_utils2.isFunction)(cacheItem) ? cacheItem() : cacheItem;
    if (!(_cacheItem == null ? void 0 : _cacheItem._extend)) {
      _list = _list.map((d) => {
        return {
          ...d,
          label: d.room_name || d.name,
          labelText: d.room_name || d.name
        };
      });
    } else {
      _list = _list.map(
        (d) => (0, import_utils.formatResource)(d, _cacheItem, (item) => {
          return /* @__PURE__ */ import_react.default.createElement("div", { className: "resource-option" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "like-wrap" }), item.labelText, !item.usable && /* @__PURE__ */ import_react.default.createElement(import_AlertTriangle.default, { className: "resource-option-alert-icon" }));
        })
      );
      _list = _list.sort((a, b) => {
        if (!a.usable && !b.usable) {
          return 0;
        } else if (!b.usable) {
          return -1;
        } else if (!a.usable) {
          return 1;
        }
        return 0;
      });
    }
    resourcesChange == null ? void 0 : resourcesChange(_list);
    set_resources(_list);
    setLoading(false);
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Select,
    {
      allowClear: true,
      showSearch: true,
      filterOption: (str, option) => {
        return (option.labelText || "").toLowerCase().includes((str || "").toLowerCase());
      },
      loading,
      ...selectProps,
      ...resetProps,
      ref
    },
    renderOptions(_resources || [], "form_record_id", "label", "resource")
  );
});
var resources_default = Resources;
