import "react";
import { useAppState } from "../lib/useAppState.js";
import { jsx, jsxs } from "../node_modules/.pnpm/@emotion_react@11.10.4_q4mq7tnhdumujvomvw24zx33y4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
import Image from "../node_modules/.pnpm/@arco-design_web-react@2.40.1_biqbaboplfbrettd7655fr4n2y/node_modules/@arco-design/web-react/es/Image/image.js";
import Space from "../node_modules/.pnpm/@arco-design_web-react@2.40.1_biqbaboplfbrettd7655fr4n2y/node_modules/@arco-design/web-react/es/Space/index.js";
import Badge from "../node_modules/.pnpm/@arco-design_web-react@2.40.1_biqbaboplfbrettd7655fr4n2y/node_modules/@arco-design/web-react/es/Badge/index.js";
import List from "../node_modules/.pnpm/@arco-design_web-react@2.40.1_biqbaboplfbrettd7655fr4n2y/node_modules/@arco-design/web-react/es/List/index.js";
import Avatar from "../node_modules/.pnpm/@arco-design_web-react@2.40.1_biqbaboplfbrettd7655fr4n2y/node_modules/@arco-design/web-react/es/Avatar/index.js";
const ReadForm = ({
  columns,
  values = {}
}) => {
  useAppState();
  return /* @__PURE__ */ jsx("div", {
    className: "",
    children: /* @__PURE__ */ jsx("dl", {
      children: columns == null ? void 0 : columns.map((field, index) => {
        let config = {
          type: field == null ? void 0 : field.type,
          value: values[field.name] || "",
          field,
          values
        };
        return /* @__PURE__ */ jsxs("div", {
          className: `${index % 2 === 0 ? "bg-gray-50" : "white"} px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6`,
          children: [/* @__PURE__ */ jsx("dt", {
            className: "text-sm font-medium text-gray-500",
            children: (field == null ? void 0 : field.label) || (field == null ? void 0 : field.name)
          }), /* @__PURE__ */ jsx("dd", {
            className: "mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2",
            children: /* @__PURE__ */ jsx(ValueRender, {
              ...config
            })
          })]
        }, (field == null ? void 0 : field.label) || (field == null ? void 0 : field.name));
      })
    })
  });
};
const ValueRender = ({
  type,
  value,
  field = {},
  values = {}
}) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  if (type === "status") {
    type = "select";
  }
  if (type === "array") {
    type = "list";
  }
  if (type === "image" || type === "images") {
    type = "upload";
  }
  switch (type) {
    case "switch": {
      return value ? "\u662F" : "\u5426";
    }
    case "date": {
      return value && formatDate(value) || "-";
    }
    case "list": {
      console.log("value", value);
      return /* @__PURE__ */ jsx(List, {
        style: {
          width: 600
        },
        dataSource: Array.isArray(value) ? value : [value],
        render: (item, index) => /* @__PURE__ */ jsx(List.Item, {
          actions: [/* @__PURE__ */ jsx(Link, {
            className: "px-4 cursor-pointer text-sky-400",
            to: `/admin/crud/Product/item/${item.id}`,
            children: "\u67E5\u770B"
          })],
          children: /* @__PURE__ */ jsx(List.Item.Meta, {
            avatar: item.image && /* @__PURE__ */ jsx(Avatar, {
              shape: "square",
              children: "A"
            }),
            title: `${item.name || item.title || item.label || ""} ${item.price && `${item.price}\u5143` || ""}`,
            description: item.value || item.intro || item.description || ""
          })
        }, index)
      });
    }
    case "number": {
      return Number(value).toLocaleString();
    }
    case "select": {
      let data = value && ((_a = field == null ? void 0 : field.options) == null ? void 0 : _a.filter((i) => (i == null ? void 0 : i.value) === value)[0]) || {};
      if (field.options) {
        return /* @__PURE__ */ jsx(Badge, {
          className: data.className,
          children: data.label
        });
      }
      if (field && values && values[(_b = field == null ? void 0 : field.sourceConfig) == null ? void 0 : _b.sourceColumn] && values[(_c = field == null ? void 0 : field.sourceConfig) == null ? void 0 : _c.sourceColumn][(_d = field == null ? void 0 : field.sourceConfig) == null ? void 0 : _d.sourceLabel]) {
        return /* @__PURE__ */ jsx(Link, {
          className: "text-sky-400",
          to: `/admin/crud/${(_e = field == null ? void 0 : field.sourceConfig) == null ? void 0 : _e.sourceName}/item/${(_g = values[(_f = field.sourceConfig) == null ? void 0 : _f.sourceColumn]) == null ? void 0 : _g.id}`,
          children: values[(_h = field.sourceConfig) == null ? void 0 : _h.sourceColumn][(_i = field.sourceConfig) == null ? void 0 : _i.sourceLabel]
        });
      }
      return value || "-";
    }
    case "upload": {
      if (value && value.constructor === Array) {
        return /* @__PURE__ */ jsx(Space, {
          direction: "vertical",
          children: /* @__PURE__ */ jsx(Image.PreviewGroup, {
            infinite: true,
            children: /* @__PURE__ */ jsx(Space, {
              children: value == null ? void 0 : value.map((src, index) => /* @__PURE__ */ jsx("div", {
                className: "",
                children: src && /* @__PURE__ */ jsx(Image, {
                  src: getReplaceURL(src.url || src),
                  width: 50,
                  alt: field.label || field.title || ""
                })
              }, index))
            })
          })
        });
      } else {
        return value ? /* @__PURE__ */ jsx(Image, {
          src: getReplaceURL(value),
          width: 50,
          alt: field.label || field.title || ""
        }) : "-";
      }
    }
    default: {
      return typeof value === "object" ? JSON.stringify(value) : value.toString() || "-";
    }
  }
};
const formatDate = (data) => {
  var date = new Date(Date.parse(data));
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  return [year, month, day].map(formatNumber).join("/") + " " + [hour, minute].map(formatNumber).join(":");
};
const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : "0" + n;
};
function getReplaceURL(item) {
  let trimBucket = "6f6e-one-4gipvd7sc515a40b-1309259962";
  let searchValue = `cloud://one-4gipvd7sc515a40b.${trimBucket}`;
  let replaceValue = `https://${trimBucket}.tcb.qcloud.la`;
  if (/^cloud:\/\//.test(item)) {
    item = item.replace(new RegExp(searchValue, "g"), replaceValue);
  }
  return item;
}
export {
  ReadForm
};
//# sourceMappingURL=ReadForm.js.map
