import {
  Filter
} from "./chunk-LAIAW5BT.js";
import {
  TextInput
} from "./chunk-H5MRGJQP.js";
import {
  Spacing
} from "./chunk-E5DUKMCU.js";
import {
  Button
} from "./chunk-SIPHI6JZ.js";
import {
  __spreadProps,
  __spreadValues
} from "./chunk-W7FAWX6T.js";

// src/components/DataManagerTable/IdFilter.tsx
import styled from "@emotion/styled";
import { Fragment, jsx, jsxs } from "@emotion/react/jsx-runtime";
var DEFAULT_ERROR_MESSAGE = 'Please ensure ID is formatted correctly, e.g. "User:01896631-99e6-04f8-0000-6df5455fabcd"';
var isIdFilterState = (state) => state.type === "id" /* ID */;
var onValidate = (state, isMulti) => {
  var _a;
  const { value, allowedEntities } = state;
  if (!value) {
    return __spreadValues({}, state);
  }
  let uuid = value;
  if (allowedEntities) {
    const entityPrefix = allowedEntities.find(
      (entity) => value.startsWith(`${entity}:`)
    );
    const hasValidEntityPrefix = !!entityPrefix;
    if (!hasValidEntityPrefix) {
      return false;
    }
    uuid = value.replace(`${entityPrefix}:`, "");
  }
  const isUuid = uuid.match(
    /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
  );
  if (!isUuid) {
    return false;
  }
  let updatedAppliedValues = [];
  if (isMulti) {
    updatedAppliedValues = ((_a = state.appliedValues) == null ? void 0 : _a.filter((appliedValue) => appliedValue !== value)) || [];
  }
  updatedAppliedValues.push(uuid);
  return __spreadProps(__spreadValues({}, state), {
    value: uuid,
    appliedValues: updatedAppliedValues
  });
};
var getDefaultIdFilterState = (allowedEntities) => ({
  type: "id" /* ID */,
  value: "",
  errorMessage: "",
  allowedEntities,
  isApplied: false,
  onValidate: (filterState, isMulti) => onValidate(filterState, isMulti)
});
var cleanValue = (value) => {
  return value.trim();
};
var IdFilter = ({
  updateFilterState,
  state,
  placeholder,
  label
}) => {
  var _a;
  const handleChange = (rawValue) => {
    var _a2;
    const value = cleanValue(rawValue);
    updateFilterState(__spreadProps(__spreadValues({}, state), {
      value,
      isApplied: !!value || !!((_a2 = state.appliedValues) == null ? void 0 : _a2.length),
      errorMessage: void 0
    }));
  };
  const appliedValueButtons = (_a = state.appliedValues) == null ? void 0 : _a.map((value) => {
    return /* @__PURE__ */ jsx(
      Button,
      {
        kind: "primary",
        size: "ExtraSmall",
        text: value,
        icon: { name: "Close" },
        iconSide: "right",
        onClick: () => {
          var _a2;
          updateFilterState(__spreadProps(__spreadValues({}, state), {
            appliedValues: (_a2 = state.appliedValues) == null ? void 0 : _a2.filter(
              (appliedValue) => appliedValue !== value
            )
          }));
        }
      },
      value
    );
  });
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(Filter, { label, children: /* @__PURE__ */ jsx(
      TextInput,
      {
        placeholder: placeholder || "",
        onChange: handleChange,
        value: state.value,
        error: state.errorMessage
      }
    ) }),
    !!(appliedValueButtons == null ? void 0 : appliedValueButtons.length) ? /* @__PURE__ */ jsx(AppliedButtonsContainer, { children: appliedValueButtons }) : null
  ] });
};
var AppliedButtonsContainer = styled.div`
  margin-top: ${Spacing.px.sm};
  display: flex;
  gap: ${Spacing.px.xs};
  flex-wrap: wrap;
`;

export {
  DEFAULT_ERROR_MESSAGE,
  isIdFilterState,
  getDefaultIdFilterState,
  IdFilter
};
