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/plus/saasAttendance/settings/index.tsx
var settings_exports = {};
__export(settings_exports, {
  default: () => settings_default,
  settingsContext: () => settingsContext
});
module.exports = __toCommonJS(settings_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_ahooks = require("ahooks");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_utils2 = require("../../../utils");
var import_locales = __toESM(require("../locales"));
var import_BasicInfoTab = __toESM(require("./BasicInfoTab"));
var import_PermissonTab = __toESM(require("./PermissonTab"));
var import_ShiftScheduleTab = __toESM(require("./ShiftScheduleTab"));
var import_ClockInMethodTab = __toESM(require("./ClockInMethodTab"));
var import_CorrectionPolicyTab = __toESM(require("./CorrectionPolicyTab"));
var import_serve = require("./serve");
var import_index = require("./index.less");
var TAB_CONFIGS = [
  {
    key: "basic",
    icon: "pisell2-settings-02",
    labelKey: "pisell2.attendance.group-settings.basic-info"
  },
  {
    key: "shift",
    icon: "pisell2-calendar",
    labelKey: "pisell2.attendance.group-settings.shift-schedule"
  },
  {
    key: "clock",
    icon: "pisell2-marker-pin-01",
    labelKey: "pisell2.attendance.group-settings.clock-in-method"
  },
  {
    key: "correction",
    icon: "pisell2-clock",
    labelKey: "pisell2.attendance.group-settings.correction-policy"
  },
  {
    key: "permission",
    icon: "pisell2-user-01",
    labelKey: "pisell2.attendance.group-settings.data-permission"
  }
];
var generateSimpleUUID = () => {
  return Math.random().toString(36).substring(2) + Date.now().toString(36);
};
var settingsContext = (0, import_react.createContext)({
  members: []
});
var Settings = (0, import_react.forwardRef)((props, ref) => {
  var _a, _b, _c, _d;
  const { value, customScrollParent } = props;
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(import_locales.default, ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en");
  if ((_d = (_c = context == null ? void 0 : context.appHelper) == null ? void 0 : _c.utils) == null ? void 0 : _d.request) {
    import_utils2.request.setRequest(context.appHelper.utils.request);
  }
  const [form] = import_antd.Form.useForm();
  const [activeKey, setActiveKey] = (0, import_react.useState)("basic");
  const uniqueIdRef = (0, import_react.useRef)(generateSimpleUUID().substring(0, 8));
  const _uniqueId = uniqueIdRef.current;
  const isManualChangeRef = (0, import_react.useRef)(false);
  (0, import_react.useEffect)(() => {
    document.body.id = "body";
  }, []);
  const { data: membersResp } = (0, import_ahooks.useRequest)(import_serve.getAllMembers, {
    defaultParams: [
      {
        page: 1,
        size: 499,
        with: ["account"]
      }
    ]
  });
  (0, import_react.useEffect)(() => {
    if (!value) return;
    const valueAny = value;
    const rawMembers = valueAny == null ? void 0 : valueAny.members;
    const memberIdsFromMembers = Array.isArray(rawMembers) ? Array.from(
      new Set(
        rawMembers.map((item) => item == null ? void 0 : item.tenant_account_id).filter((id) => typeof id === "number" && !Number.isNaN(id))
      )
    ) : [];
    const existingMemberIds = Array.isArray(valueAny == null ? void 0 : valueAny.member_ids) ? valueAny.member_ids : [];
    const resolvedMemberIds = existingMemberIds.length > 0 ? existingMemberIds : memberIdsFromMembers;
    const { members, ...rest } = valueAny;
    form.setFieldsValue({
      ...rest,
      member_ids: resolvedMemberIds
    });
  }, [value, form]);
  const debounceIndicator = (0, import_react.useCallback)((delay) => {
    isManualChangeRef.current = true;
    const timeoutId = setTimeout(() => {
      isManualChangeRef.current = false;
    }, delay);
    return () => clearTimeout(timeoutId);
  }, []);
  (0, import_react.useImperativeHandle)(ref, () => ({
    ...form
  }));
  const switchTab = (0, import_react.useCallback)(
    (key) => {
      setActiveKey(key);
      debounceIndicator(300);
      let scrollParent = null;
      const formContainer = document.getElementById(
        `saas-attendance-group-settings-${_uniqueId}`
      );
      if (typeof customScrollParent === "string") {
        scrollParent = document.querySelector(
          customScrollParent
        );
      } else {
        scrollParent = customScrollParent || formContainer;
      }
      if (!scrollParent || !formContainer) return;
      const target = document.getElementById(
        `saas-attendance-settings-section-${_uniqueId}-${key}`
      );
      if (!target) return;
      const scrollParentRect = scrollParent.getBoundingClientRect();
      const tabIndex = TAB_CONFIGS.findIndex((item) => item.key === key);
      if (tabIndex === 0) {
        scrollParent.scrollTop = 0;
      } else {
        const targetRect = target.getBoundingClientRect();
        const scrollOffset = targetRect.top - scrollParentRect.top + scrollParent.scrollTop;
        scrollParent.scrollTop = scrollOffset - 16;
      }
    },
    [customScrollParent, debounceIndicator, _uniqueId]
  );
  (0, import_react.useEffect)(() => {
    let scrollParent = null;
    const formContainer = document.getElementById(
      `saas-attendance-group-settings-${_uniqueId}`
    );
    if (typeof customScrollParent === "string") {
      scrollParent = document.querySelector(customScrollParent);
    } else {
      scrollParent = customScrollParent || formContainer;
    }
    if (!scrollParent || !formContainer) return;
    const handleScroll = () => {
      if (isManualChangeRef.current) return;
      const tabsWrapper = document.getElementById(
        `saas-attendance-settings-${_uniqueId}_tabs`
      );
      const tabsHeight = (tabsWrapper == null ? void 0 : tabsWrapper.offsetHeight) || 0;
      const scrollParentRect = scrollParent.getBoundingClientRect();
      const checkPoint = scrollParentRect.top + tabsHeight + 20;
      let currentKey = "basic";
      for (const config of TAB_CONFIGS) {
        const section = document.getElementById(
          `saas-attendance-settings-section-${_uniqueId}-${config.key}`
        );
        if (section) {
          const sectionRect = section.getBoundingClientRect();
          if (sectionRect.top <= checkPoint) {
            currentKey = config.key;
          }
        }
      }
      if (currentKey !== activeKey) {
        setActiveKey(currentKey);
      }
    };
    scrollParent.addEventListener("scroll", handleScroll);
    return () => {
      scrollParent == null ? void 0 : scrollParent.removeEventListener("scroll", handleScroll);
    };
  }, [customScrollParent, _uniqueId, activeKey]);
  const tabItems = (0, import_react.useMemo)(
    () => TAB_CONFIGS.map((config) => ({
      key: config.key,
      label: /* @__PURE__ */ import_react.default.createElement("span", { className: "saas-attendance-group-settings__tab-label" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Icon, { type: config.icon, size: 20 }), import_utils.locales.getText(config.labelKey)),
      children: null
      // 不在 tab content 中渲染内容
    })),
    []
  );
  return /* @__PURE__ */ import_react.default.createElement(settingsContext.Provider, { value: { members: (membersResp == null ? void 0 : membersResp.list) || [] } }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(
        "saas-attendance-group-settings",
        props.className
      ),
      style: props.style
    },
    props.loading ? /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__loading" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, null)) : null,
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "saas-attendance-group-settings__tabs-wrapper",
        id: `saas-attendance-settings-${_uniqueId}_tabs`
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Tabs,
        {
          activeKey,
          items: tabItems,
          onChange: switchTab,
          renderTabBar: (tabBarProps, DefaultTabBar) => /* @__PURE__ */ import_react.default.createElement(DefaultTabBar, { ...tabBarProps })
        }
      )
    ),
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form,
      {
        id: `saas-attendance-group-settings-${_uniqueId}`,
        className: "saas-attendance-group-settings__form",
        form,
        layout: "vertical",
        size: "large",
        onValuesChange: props.onValuesChange,
        onFinish: props.onFinish,
        scrollToFirstError: true
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_BasicInfoTab.default,
        {
          id: `saas-attendance-settings-section-${_uniqueId}-basic`
        }
      ),
      /* @__PURE__ */ import_react.default.createElement(
        import_ShiftScheduleTab.default,
        {
          id: `saas-attendance-settings-section-${_uniqueId}-shift`
        }
      ),
      /* @__PURE__ */ import_react.default.createElement(
        import_ClockInMethodTab.default,
        {
          form,
          id: `saas-attendance-settings-section-${_uniqueId}-clock`
        }
      ),
      /* @__PURE__ */ import_react.default.createElement(
        import_CorrectionPolicyTab.default,
        {
          form,
          id: `saas-attendance-settings-section-${_uniqueId}-correction`
        }
      ),
      /* @__PURE__ */ import_react.default.createElement(
        import_PermissonTab.default,
        {
          id: `saas-attendance-settings-section-${_uniqueId}-permission`
        }
      )
    )
  ));
});
var settings_default = Settings;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  settingsContext
});
