/*
 * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Error } from "@blueprintjs/icons";
import React, { useCallback, useState } from "react";
import { ActionButton } from "../base-components/action-button/ActionButton.js";
import { useObjectTableLabels } from "./ObjectTableLabels.js";
import styles from "./TableEditContainer.module.css";
export function TableEditContainer({
  editableConfig,
  hasFocusedRow
}) {
  const {
    cellEdits,
    onSubmitEdits,
    clearEdits,
    editModeState,
    validationErrors
  } = editableConfig;
  const labels = useObjectTableLabels();
  const hasEdits = Object.keys(cellEdits ?? {}).length > 0;
  const hasValidationError = (validationErrors?.size ?? 0) > 0;
  const [isSubmitting, setIsSubmitting] = useState(false);
  const isInEditMode = editModeState.isActive;
  const canToggleEditMode = editModeState.type === "manual";
  const handleSubmitEdits = useCallback(async () => {
    setIsSubmitting(true);
    try {
      const success = await onSubmitEdits?.();
      if (success) {
        clearEdits?.();
        if (editModeState?.type === "manual") {
          editModeState.setActive(false);
        }
      }
    } finally {
      setIsSubmitting(false);
    }
  }, [editModeState, onSubmitEdits, clearEdits]);
  const handleCancelEdits = useCallback(() => {
    clearEdits?.();
    if (editModeState?.type === "manual") {
      editModeState.setActive(false);
    }
  }, [clearEdits, editModeState]);
  const handleEnterEditMode = useCallback(() => {
    if (editModeState?.type === "manual") {
      editModeState.setActive(true);
    }
  }, [editModeState]);
  return /*#__PURE__*/React.createElement("div", {
    className: styles.tableEditContainer
  }, hasEdits || hasValidationError ? /*#__PURE__*/React.createElement("div", {
    className: styles.editsInfoContainer
  }, hasEdits && /*#__PURE__*/React.createElement("div", {
    className: styles.modificationCount
  }, labels.editFooterModificationCount(cellEdits ? Object.keys(cellEdits).length : 0)), hasEdits && hasValidationError && /*#__PURE__*/React.createElement("div", {
    className: styles.divider
  }), hasValidationError && /*#__PURE__*/React.createElement("div", {
    className: styles.validationError
  }, /*#__PURE__*/React.createElement(Error, {
    className: styles.errorIcon
  }), labels.editFooterValidationError)) : isInEditMode && !hasFocusedRow && /*#__PURE__*/React.createElement("div", {
    className: styles.placeholder
  }, labels.editFooterSelectRowToEdit), /*#__PURE__*/React.createElement("div", {
    className: styles.editButtons
  }, !isInEditMode && canToggleEditMode && /*#__PURE__*/React.createElement(ActionButton, {
    variant: "primary",
    onClick: handleEnterEditMode
  }, labels.editFooterEditTable), isInEditMode && canToggleEditMode && /*#__PURE__*/React.createElement(ActionButton, {
    variant: "secondary",
    onClick: handleCancelEdits
  }, labels.editFooterCancel), isInEditMode && !!onSubmitEdits && /*#__PURE__*/React.createElement(ActionButton, {
    variant: "primary",
    onClick: handleSubmitEdits,
    disabled: !hasEdits || isSubmitting || hasValidationError
  }, labels.editFooterSubmitEdits)));
}
//# sourceMappingURL=TableEditContainer.js.map