import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { Component, Field, useDerivedField, useEnvironment } from "@contember/binding";
import { SlugInput } from "@contember/ui";
import slugify from "@sindresorhus/slugify";
import { useCallback, useMemo } from "react";
import { useFieldControl } from "./useFieldControl.js";
import { SimpleRelativeSingleField } from "../auxiliary/SimpleRelativeSingleField/SimpleRelativeSingleField.js";
const useNormalizedPrefix = (value) => {
  const environment = useEnvironment();
  return useMemo(() => typeof value === "function" ? value(environment) : value ?? "", [value, environment]);
};
const SlugField = Component(
  (props) => /* @__PURE__ */ jsxDEV(SlugFieldInner, { ...props }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/fields/SlugField.tsx",
    lineNumber: 38,
    columnNumber: 11
  }, globalThis),
  (props) => /* @__PURE__ */ jsxDEV(Fragment, { children: [
    /* @__PURE__ */ jsxDEV(Field, { field: props.derivedFrom }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/fields/SlugField.tsx",
      lineNumber: 40,
      columnNumber: 3
    }, globalThis),
    /* @__PURE__ */ jsxDEV(SlugFieldInner, { ...props }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/fields/SlugField.tsx",
      lineNumber: 41,
      columnNumber: 3
    }, globalThis)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/fields/SlugField.tsx",
    lineNumber: 39,
    columnNumber: 11
  }, globalThis)
);
const SlugFieldInner = SimpleRelativeSingleField(
  (fieldMetadata, {
    unpersistedHardPrefix,
    persistedHardPrefix,
    persistedSoftPrefix,
    linkToExternalUrl,
    derivedFrom,
    field,
    ...props
  }) => {
    const normalizedUnpersistedHardPrefix = useNormalizedPrefix(unpersistedHardPrefix);
    const normalizedPersistedHardPrefix = useNormalizedPrefix(persistedHardPrefix);
    const normalizedPersistedSoftPrefix = useNormalizedPrefix(persistedSoftPrefix);
    const inputProps = useFieldControl({
      ...props,
      fieldMetadata,
      parse: (val, field2) => {
        const parsedValue = val ?? null;
        return parsedValue !== null ? `${normalizedPersistedHardPrefix}${parsedValue}` : null;
      },
      format: (value) => typeof value === "string" ? value.substring(normalizedPersistedHardPrefix.length) : ""
    });
    const transform = useCallback(
      (driverFieldValue) => {
        if (driverFieldValue === null) {
          return null;
        }
        const slugValue = slugify(driverFieldValue);
        return `${normalizedPersistedHardPrefix}${normalizedPersistedSoftPrefix}${slugValue}`;
      },
      [normalizedPersistedHardPrefix, normalizedPersistedSoftPrefix]
    );
    useDerivedField(derivedFrom, field, transform);
    const hardPrefix = normalizedUnpersistedHardPrefix + normalizedPersistedHardPrefix;
    const fullValue = hardPrefix + inputProps.value;
    return /* @__PURE__ */ jsxDEV(
      SlugInput,
      {
        ...inputProps,
        prefix: hardPrefix,
        link: linkToExternalUrl ? fullValue : void 0
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/fields/SlugField.tsx",
        lineNumber: 86,
        columnNumber: 4
      },
      globalThis
    );
  },
  "SlugField"
);
export {
  SlugField,
  SlugFieldInner
};
//# sourceMappingURL=SlugField.js.map
