"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
var Feedback_stories_exports = {};
__export(Feedback_stories_exports, {
  ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError: () => ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError,
  AutoGeneratedDataOnly: () => AutoGeneratedDataOnly,
  Like: () => Like,
  Liked: () => Liked,
  Primary: () => Primary,
  WithDeleteDialog: () => WithDeleteDialog,
  default: () => Feedback_stories_default
});
module.exports = __toCommonJS(Feedback_stories_exports);
var import_react = require("@nova/react");
var import_apollo = require("@nova/react-test-utils/apollo");
var import_test = require("@storybook/test");
var import_Feedback = require("./Feedback");
var React = __toESM(require("react"), 1);
var import_apolloCacheConfig = require("../../testing-utils/apolloCacheConfig");
var import_schema = require("../../testing-utils/schema");
var import_resolvers = require("../../testing-utils/resolvers");
const meta = {
  component: import_Feedback.FeedbackComponent,
  decorators: [(0, import_apollo.getNovaDecorator)(import_schema.schema, { cache: import_apolloCacheConfig.cacheConfig })],
  parameters: {
    novaEnvironment: {
      query: import_react.graphql`
        query FeedbackStoryQuery($id: ID!) @relay_test_operation {
          feedback(id: $id) {
            ...Feedback_feedbackFragment
          }
        }
      `,
      variables: { id: "42" },
      referenceEntries: {
        feedback: (data) => data == null ? void 0 : data.feedback
      },
      resolvers: {
        Node: import_resolvers.defaultNodeResolver
      }
    }
  }
};
var Feedback_stories_default = meta;
const AutoGeneratedDataOnly = {};
const Primary = {
  parameters: {
    novaEnvironment: {
      resolvers: {
        Feedback: () => sampleFeedback
      }
    }
  }
};
const Liked = {
  parameters: {
    novaEnvironment: {
      resolvers: {
        Feedback: () => __spreadProps(__spreadValues({}, sampleFeedback), {
          doesViewerLike: true
        })
      }
    }
  }
};
const Like = {
  parameters: {
    novaEnvironment: {
      resolvers: {
        Feedback: () => sampleFeedback,
        FeedbackLikeMutationResult: () => ({
          feedback: __spreadProps(__spreadValues({}, sampleFeedback), {
            doesViewerLike: true
          })
        })
      }
    }
  },
  play: (context) => __async(void 0, null, function* () {
    const container = (0, import_test.within)(context.canvasElement);
    const likeButton = yield container.findByRole("button", { name: "Like" });
    yield import_test.userEvent.click(likeButton);
    yield container.findByRole("button", { name: "Unlike" });
  })
};
const mockOnError = (0, import_test.fn)();
const ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError = {
  parameters: {
    novaEnvironment: {
      enableQueuedMockResolvers: false
    },
    errorBoundary: {
      onError: mockOnError
    }
  },
  play: (context) => __async(void 0, null, function* () {
    const {
      graphql: { mock }
    } = (0, import_apollo.getNovaEnvironmentForStory)(context);
    yield (0, import_test.waitFor)(() => __async(void 0, null, function* () {
      const operation = mock.getMostRecentOperation();
      yield (0, import_test.expect)(operation).toBeDefined();
    }));
    yield mock.rejectMostRecentOperation(new Error("Query failed"));
    yield (0, import_test.waitFor)(() => {
      (0, import_test.expect)(mockOnError).toHaveBeenCalledTimes(1);
    });
    const call = mockOnError.mock.calls[0];
    (0, import_test.expect)(call[0].message).toBe("Query failed");
  })
};
const FeedbackWithDeleteDialog = (props) => {
  const [open, setOpen] = React.useState(false);
  const [text, setText] = React.useState("");
  return /* @__PURE__ */ React.createElement(
    import_apollo.EventingInterceptor,
    {
      eventMap: {
        onDeleteFeedback: (eventWrapper) => {
          setOpen(true);
          setText(eventWrapper.event.data().feedbackText);
          return Promise.resolve(void 0);
        },
        feedbackTelemetry: (eventWrapper) => {
          console.log("Telemetry event", eventWrapper.event.data());
          return Promise.resolve(eventWrapper);
        }
      }
    },
    /* @__PURE__ */ React.createElement(import_Feedback.FeedbackComponent, __spreadValues({}, props)),
    /* @__PURE__ */ React.createElement("dialog", { open }, /* @__PURE__ */ React.createElement("button", { onClick: () => setOpen(false) }, "Cancel"), 'Are you sure you want to delete feedback "', text, '"')
  );
};
const WithDeleteDialog = __spreadProps(__spreadValues({}, Primary), {
  render: (args) => /* @__PURE__ */ React.createElement(FeedbackWithDeleteDialog, __spreadValues({}, args)),
  play: (context) => __async(void 0, null, function* () {
    const container = (0, import_test.within)(context.canvasElement);
    const deleteButton = yield container.findByRole("button", {
      name: "Delete feedback"
    });
    yield import_test.userEvent.click(deleteButton);
    const dialog = yield container.findByRole("dialog");
    yield (0, import_test.expect)(dialog).toBeInTheDocument();
  })
});
const sampleFeedback = {
  id: "feedback:42",
  message: {
    text: "Feedback title"
  },
  doesViewerLike: false
};
