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/authentication/admin/ChannelAuthSetting/SaaSManagermentAuth/index.tsx
var SaaSManagermentAuth_exports = {};
__export(SaaSManagermentAuth_exports, {
  default: () => SaaSManagermentAuth_default
});
module.exports = __toCommonJS(SaaSManagermentAuth_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../../../../hooks/useEngineContext"));
var import_PageSetting = __toESM(require("../PageSetting"));
var import_locales = __toESM(require("../locales"));
var import_index = require("./index.less");
var { Title, Paragraph } = import_antd.Typography;
var SaaSManagermentAuth = ({
  className,
  style,
  value,
  defaultActiveTab,
  visibleTabs,
  showRegisterPage = true,
  onSave
}) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  const { useParams } = context.appHelper.utils.getApp().history;
  const { channel } = useParams();
  const { request } = context.appHelper.utils.getApp();
  const [loading, setLoading] = (0, import_react.useState)(true);
  const [channelData, setChannelData] = (0, import_react.useState)(null);
  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");
  const allTabs = ["pageSetting"];
  const saveData = (data) => {
    debugger;
    request.post(`/api/tenant_channel:update?filter=%7B%22channel_code%22%3A%22${channel}%22%7D`, {
      ...channelData,
      metadata: {
        ...channelData == null ? void 0 : channelData.metadata,
        register_login: data
      }
    }, {
      isNocobase: true,
      headers: {
        "x-data-source": "pisell2"
      }
    }).then((res) => {
      import_antd.message.success(import_utils.locales.getText("channel.auth.setting.message.saveSuccess"));
    }).catch((error) => {
      console.error("Error saving channel data:", error);
      import_antd.message.error(import_utils.locales.getText("channel.auth.setting.message.saveError"));
    });
  };
  (0, import_react.useEffect)(() => {
    if (channel) {
      setLoading(true);
      request.get(`/api/tenant_channel:get?filter=%7B%22channel_code%22%3A%22${channel}%22%7D&appends=channel`, {}, {
        isNocobase: true,
        headers: {
          "x-data-source": "pisell2"
        }
      }).then((res) => {
        if (res == null ? void 0 : res.data) {
          setChannelData(res == null ? void 0 : res.data);
        }
        console.log("res_saas_management_auth", res);
        setLoading(false);
      }).catch((error) => {
        console.error("Error fetching channel data:", error);
        setLoading(false);
      });
    }
  }, [request, channel]);
  const displayTabs = (0, import_react.useMemo)(() => {
    if (visibleTabs && visibleTabs.length > 0) {
      return allTabs.filter((tab) => visibleTabs.includes(tab));
    }
    return allTabs;
  }, [visibleTabs]);
  const initialActiveTab = (0, import_react.useMemo)(() => {
    if (defaultActiveTab && displayTabs.includes(defaultActiveTab)) {
      return defaultActiveTab;
    }
    return displayTabs[0] || "pageSetting";
  }, [defaultActiveTab, displayTabs]);
  const [activeTab, setActiveTab] = (0, import_react.useState)(initialActiveTab);
  const [internalValue, setInternalValue] = (0, import_react.useState)(
    value || {}
  );
  (0, import_react.useEffect)(() => {
    if (value) {
      setInternalValue(value);
    }
  }, [value]);
  (0, import_react.useEffect)(() => {
    var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
    if ((_a2 = channelData == null ? void 0 : channelData.metadata) == null ? void 0 : _a2.register_login) {
      const { page } = channelData.metadata.register_login;
      const pageSettingValue = {};
      if (page) {
        pageSettingValue.registerPage = {
          backgroundImage: ((_b2 = page.register) == null ? void 0 : _b2.background_image_url) || "",
          termsConditions: ((_d = (_c = page.register) == null ? void 0 : _c.terms) == null ? void 0 : _d.map((term) => {
            var _a3, _b3, _c2, _d2, _e2, _f2;
            return {
              name: {
                original: ((_a3 = term.name) == null ? void 0 : _a3.original) || "",
                en: ((_b3 = term.name) == null ? void 0 : _b3.en) || "",
                "zh-CN": ((_c2 = term.name) == null ? void 0 : _c2["zh-CN"]) || "",
                "zh-HK": ((_d2 = term.name) == null ? void 0 : _d2["zh-HK"]) || "",
                ja: ((_e2 = term.name) == null ? void 0 : _e2["ja"]) || "",
                pt: ((_f2 = term.name) == null ? void 0 : _f2["pt"]) || ""
              },
              url: term.url || ""
            };
          })) || []
        };
        pageSettingValue.loginPage = {
          sameAsRegister: ((_e = page.login) == null ? void 0 : _e.same_as_register) ?? true,
          settings: ((_f = page.login) == null ? void 0 : _f.same_as_register) ? void 0 : {
            backgroundImage: ((_g = page.login) == null ? void 0 : _g.background_image_url) || "",
            termsConditions: ((_i = (_h = page.login) == null ? void 0 : _h.terms) == null ? void 0 : _i.map((term) => {
              var _a3, _b3, _c2, _d2, _e2, _f2;
              return {
                name: {
                  original: ((_a3 = term.name) == null ? void 0 : _a3.original) || "",
                  en: ((_b3 = term.name) == null ? void 0 : _b3.en) || "",
                  "zh-CN": ((_c2 = term.name) == null ? void 0 : _c2["zh-CN"]) || "",
                  "zh-HK": ((_d2 = term.name) == null ? void 0 : _d2["zh-HK"]) || "",
                  ja: ((_e2 = term.name) == null ? void 0 : _e2["ja"]) || "",
                  pt: ((_f2 = term.name) == null ? void 0 : _f2["pt"]) || ""
                },
                url: term.url || ""
              };
            })) || []
          }
        };
      }
      setInternalValue({
        pageSetting: pageSettingValue
      });
    }
  }, [channelData]);
  const handlePageSettingChange = (0, import_react.useCallback)((pageSettingValue) => {
    setInternalValue((prev) => {
      const hasPageSettingChanged = JSON.stringify(prev.pageSetting) !== JSON.stringify(pageSettingValue);
      if (hasPageSettingChanged) {
        return {
          ...prev,
          pageSetting: pageSettingValue
        };
      }
      return prev;
    });
  }, []);
  const transformToNewFormat = (value2) => {
    var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k;
    const { pageSetting } = value2;
    console.log("pageSetting data:", pageSetting);
    const pageData = {
      register: {
        background_image_url: ((_a2 = pageSetting == null ? void 0 : pageSetting.registerPage) == null ? void 0 : _a2.backgroundImage) || "",
        terms: ((_c = (_b2 = pageSetting == null ? void 0 : pageSetting.registerPage) == null ? void 0 : _b2.termsConditions) == null ? void 0 : _c.map((term) => ({
          name: {
            original: term.name.original || "",
            en: term.name.en || "",
            "zh-CN": term.name["zh-CN"] || "",
            "zh-HK": term.name["zh-HK"] || "",
            ja: term.name["ja"] || "",
            pt: term.name["pt"] || ""
          },
          url: term.url || ""
        }))) || []
      },
      login: {
        same_as_register: ((_d = pageSetting == null ? void 0 : pageSetting.loginPage) == null ? void 0 : _d.sameAsRegister) ?? true,
        background_image_url: ((_e = pageSetting == null ? void 0 : pageSetting.loginPage) == null ? void 0 : _e.sameAsRegister) ? null : ((_g = (_f = pageSetting == null ? void 0 : pageSetting.loginPage) == null ? void 0 : _f.settings) == null ? void 0 : _g.backgroundImage) || null,
        terms: ((_h = pageSetting == null ? void 0 : pageSetting.loginPage) == null ? void 0 : _h.sameAsRegister) ? [] : ((_k = (_j = (_i = pageSetting == null ? void 0 : pageSetting.loginPage) == null ? void 0 : _i.settings) == null ? void 0 : _j.termsConditions) == null ? void 0 : _k.map((term) => ({
          name: {
            original: term.name.original || "",
            en: term.name.en || "",
            "zh-CN": term.name["zh-CN"] || "",
            "zh-HK": term.name["zh-HK"] || "",
            ja: term.name["ja"] || "",
            pt: term.name["pt"] || ""
          },
          url: term.url || ""
        }))) || []
      }
    };
    return {
      page: pageData
    };
  };
  const handleSave = () => {
    const transformedValue = transformToNewFormat(internalValue);
    console.log("transformedValue", transformedValue);
    saveData(transformedValue);
  };
  const tabItems = (0, import_react.useMemo)(() => [
    {
      key: "pageSetting",
      label: import_utils.locales.getText("channel.auth.setting.tab.pageSetting"),
      children: /* @__PURE__ */ import_react.default.createElement(
        import_PageSetting.default,
        {
          value: internalValue.pageSetting,
          onChange: handlePageSettingChange,
          showRegisterPage
        }
      )
    }
  ], [internalValue, showRegisterPage]);
  if (loading) {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("channel-auth-setting", className), style: { ...style, display: "flex", justifyContent: "center", alignItems: "center", minHeight: "400px" } }, /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { size: "large" }));
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("saasmanagermentauth", className), style }, /* @__PURE__ */ import_react.default.createElement("div", { className: "saasmanagermentauth-header" }, /* @__PURE__ */ import_react.default.createElement(Title, { level: 1, className: "saasmanagermentauth-title" }, import_utils.locales.getText("channel.auth.setting.title")), /* @__PURE__ */ import_react.default.createElement(Paragraph, { className: "saasmanagermentauth-description" }, import_utils.locales.getText("channel.auth.setting.description"))), /* @__PURE__ */ import_react.default.createElement("div", { className: "saasmanagermentauth-tabs" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Tabs,
    {
      activeKey: activeTab,
      onChange: setActiveTab,
      items: tabItems
    }
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "saasmanagermentauth-footer" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "primary", onClick: handleSave }, import_utils.locales.getText("channel.auth.setting.button.save"))));
};
var SaaSManagermentAuth_default = SaaSManagermentAuth;
