Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("../../chunk-BN4qSVkS.cjs");
const require_components_Button = require("../Button.cjs");
const require_components_TextInput = require("../TextInput.cjs");
const require_components_DataManagerTable_Filter = require("./Filter.cjs");
require("./filters.cjs");
const require_components_DataManagerTable_AppliedButtonsContainer = require("./AppliedButtonsContainer.cjs");
let _emotion_react_jsx_runtime = require("@emotion/react/jsx-runtime");
//#region src/components/DataManagerTable/IdFilter.tsx
const DEFAULT_ERROR_MESSAGE = "Please ensure ID is formatted correctly, e.g. \"User:01896631-99e6-04f8-0000-6df5455fabcd\"";
const isIdFilterState = (state) => state.type === "id";
/**
* Validates the filter state and updates the filter state.
*/
const onValidate = (state, isMulti) => {
	const { value, allowedEntities } = state;
	if (!value) return { ...state };
	let uuid = value;
	if (allowedEntities) {
		const entityPrefix = allowedEntities.find((entity) => value.startsWith(`${entity}:`));
		if (!!!entityPrefix) return false;
		uuid = value.replace(`${entityPrefix}:`, "");
	}
	if (!uuid.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)) return false;
	let updatedAppliedValues = [];
	if (isMulti) updatedAppliedValues = state.appliedValues?.filter((appliedValue) => appliedValue !== value) || [];
	updatedAppliedValues.push(uuid);
	return {
		...state,
		value: uuid,
		appliedValues: updatedAppliedValues
	};
};
const getDefaultIdFilterState = (allowedEntities) => ({
	type: "id",
	value: "",
	errorMessage: "",
	allowedEntities,
	isApplied: false,
	onValidate: (filterState, isMulti) => onValidate(filterState, isMulti)
});
const cleanValue = (value) => {
	return value.trim();
};
const IdFilter = ({ updateFilterState, state, placeholder, label }) => {
	const handleChange = (rawValue) => {
		const value = cleanValue(rawValue);
		updateFilterState({
			...state,
			value,
			isApplied: !!value || !!state.appliedValues?.length,
			errorMessage: void 0
		});
	};
	const appliedValueButtons = state.appliedValues?.map((value) => {
		return /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_Button.Button, {
			kind: "primary",
			size: "ExtraSmall",
			text: value,
			icon: { name: "Close" },
			iconSide: "right",
			onClick: () => {
				updateFilterState({
					...state,
					appliedValues: state.appliedValues?.filter((appliedValue) => appliedValue !== value)
				});
			}
		}, value);
	});
	return /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsxs)(_emotion_react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_DataManagerTable_Filter.Filter, {
		label,
		children: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_TextInput.TextInput, {
			placeholder: placeholder || "",
			onChange: handleChange,
			value: state.value,
			error: state.errorMessage
		})
	}), !!appliedValueButtons?.length ? /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_DataManagerTable_AppliedButtonsContainer.AppliedButtonsContainer, { children: appliedValueButtons }) : null] });
};
//#endregion
exports.DEFAULT_ERROR_MESSAGE = DEFAULT_ERROR_MESSAGE;
exports.IdFilter = IdFilter;
exports.getDefaultIdFilterState = getDefaultIdFilterState;
exports.isIdFilterState = isIdFilterState;
