'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-8277cbb3.js');
const global = require('./global-b7b1b8fe.js');
const domContextHooks_module = require('./dom-context-hooks.module-e7b6727c.js');
const index_module = require('./index.module-f8c8495f.js');
const cjs = require('./cjs-1066ec21.js');
const utils = require('./utils-6847bc06.js');
const JSS = require('./JSS-8503a151.js');
const useReferralCodes = require('./useReferralCodes-0f7948af.js');
require('./mixins-4761d472.js');
const copyTextView = require('./copy-text-view-bb2c0ea7.js');

const style = {
  Container: {
    display: "flex",
    justifyContent: "flex-end",
    alignItems: "center",
    gap: "var(--sl-spacing-small)",
    width: "100%",
  },
  TextContainer: {
    display: "flex",
    gap: "var(--sl-spacing-xx-small)",
  },
};
const sheet = JSS.createStyleSheet(style);
const styleString = sheet.toString();
function PaginationView(props) {
  const { states, callbacks, text, buttonType } = props;
  const { onNext, onPrev } = callbacks;
  const { currentPage, totalPages, loading } = states;
  if (totalPages === 1)
    return index.h(index.Fragment, null);
  return (index.h("span", { class: sheet.classes.Container, part: "sqm-base" },
    index.h("style", { type: "text/css" }, styleString),
    !loading && (index.h(index.Fragment, null,
      index.h("sl-button", { exportparts: `base: ${buttonType}button-base`, onClick: onPrev, part: "sqm-pagination-button", circle: true, disabled: currentPage === 1 },
        index.h("sl-icon", { name: "chevron-left", label: "Previous Page" })),
      index.h("div", { class: sheet.classes.TextContainer }, text.paginationText))),
    index.h("sl-button", { exportparts: `base: ${buttonType}button-base`, onClick: onNext, part: "sqm-pagination-button", circle: true, disabled: currentPage === totalPages },
      index.h("sl-icon", { name: "chevron-right", label: "Previous Page" }))));
}

function usePagination(props) {
  var _a;
  const context = index_module.Fn(useReferralCodes.REFERRAL_CODES_PAGINATION_CONTEXT);
  const currentPage = context.states.currentPage + 1;
  const totalPages = (_a = context.states) === null || _a === void 0 ? void 0 : _a.pageCount;
  return {
    states: {
      currentPage,
      totalPages,
      loading: context.states.loading,
    },
    callbacks: {
      onNext: () => { var _a, _b; return (_a = context.callbacks) === null || _a === void 0 ? void 0 : _a.setCurrentPage(((_b = context.states) === null || _b === void 0 ? void 0 : _b.currentPage) + 1); },
      onPrev: () => { var _a, _b; return (_a = context.callbacks) === null || _a === void 0 ? void 0 : _a.setCurrentPage(((_b = context.states) === null || _b === void 0 ? void 0 : _b.currentPage) - 1); },
    },
    text: {
      paginationText: global.intl.formatMessage({
        id: `paginationText`,
        defaultMessage: props.paginationText,
      }, {
        currentPage,
        totalPages,
      }),
    },
  };
}

const Pagination = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.ignored = true;
    /**
     * @uiName Pagination text
     */
    this.paginationText = "{currentPage} of {totalPages}";
    /**
     * The type of the button that is used (primary or secondary).
     * @uiName Button Type
     * @uiType string
     * @uiEnum ["primary", "secondary"]
     * @uiEnumNames ["Primary", "Secondary"]
     * @uiGroup Style
     */
    this.buttonType = "secondary";
    domContextHooks_module.h$1(this);
  }
  disconnectedCallback() { }
  render() {
    const hookProps = index_module.isDemo()
      ? useDemoPagination(utils.getProps(this))
      : usePagination(utils.getProps(this));
    return index.h(PaginationView, Object.assign({}, hookProps));
  }
};
function useDemoPagination(props) {
  const [currentPage, setCurrentPage] = domContextHooks_module.useState(1);
  return cjs.cjs({
    states: {
      currentPage,
      totalPages: 5,
      loading: false,
    },
    buttonType: props.buttonType,
    callbacks: {
      onNext: () => setCurrentPage(currentPage + 1),
      onPrev: () => setCurrentPage(currentPage - 1),
    },
    text: {
      paginationText: global.intl.formatMessage({
        id: `paginationText`,
        defaultMessage: props.paginationText || "{currentPage} of {totalPages}",
      }, {
        currentPage: currentPage,
        totalPages: 5,
      }),
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}

const MessageLinkQuery = index_module.dist.gql `
  query getReferralCode($programId: ID) {
    user: viewer {
      ... on User {
        referralCode(programId: $programId)
      }
    }
  }
`;
const WIDGET_ENGAGEMENT_EVENT = index_module.dist.gql `
  mutation loadEvent($eventMeta: UserAnalyticsEvent!) {
    createUserAnalyticsEvent(eventMeta: $eventMeta)
  }
`;
function useReferralCode(props) {
  var _a, _b;
  const { programId = index_module.H() } = props;
  const user = index_module.J();
  const engagementMedium = index_module.B();
  const contextData = index_module.Fn(useReferralCodes.REFERRAL_CODES_NAMESPACE);
  const { data } = index_module.wn(MessageLinkQuery, { programId }, !(user === null || user === void 0 ? void 0 : user.jwt) || (contextData === null || contextData === void 0 ? void 0 : contextData.referralCode) !== undefined);
  const [sendLoadEvent] = index_module.$e(WIDGET_ENGAGEMENT_EVENT);
  const [setCopied] = index_module.$e(useReferralCodes.SET_CODE_COPIED);
  const copyString = (_b = ((contextData === null || contextData === void 0 ? void 0 : contextData.referralCode) || ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.referralCode))) !== null && _b !== void 0 ? _b : 
  // Shown during loading
  "...";
  const [open, setOpen] = domContextHooks_module.useState(false);
  async function onClick() {
    if (contextData) {
      await setCopied({ referralCode: contextData.referralCode });
      contextData.refresh();
    }
    // Should well supported: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard#browser_compatibility
    // Only if called from a user-initiated event
    navigator.clipboard.writeText(copyString);
    setOpen(true);
    setTimeout(() => setOpen(false), props.tooltiplifespan);
    sendLoadEvent({
      eventMeta: {
        programId,
        id: user === null || user === void 0 ? void 0 : user.id,
        accountId: user === null || user === void 0 ? void 0 : user.accountId,
        type: "USER_REFERRAL_PROGRAM_ENGAGEMENT_EVENT",
        meta: {
          engagementMedium,
          shareMedium: "DIRECT",
        },
      },
    });
  }
  return {
    ...props,
    onClick,
    open,
    copyString: copyString,
    isCopied: contextData === null || contextData === void 0 ? void 0 : contextData.isCopied,
    showNotificationText: props.showNotificationText && !!(contextData === null || contextData === void 0 ? void 0 : contextData.singleUse),
  };
}

const ReferralCode = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    /**
     * Shown inside a tooltip after someone has successfully copied the link to their clipboard
     *
     * @uiName Tooltip text
     */
    this.tooltiptext = "Copied to Clipboard";
    /**
     * Boolean used to show notification text below input
     *
     * @uiName Show notification text
     */
    this.showNotificationText = false;
    /**
     * Shown underneath single-use promo codes when the user has already copied the code
     *
     * @uiName Notification text
     */
    this.notificationText = "You've copied this code before";
    /**
     * The number of milliseconds that the tooltip appears for
     *
     * @uiName Tooltip lifespan
     */
    this.tooltiplifespan = 1000;
    /**
     * Change the text alignment
     *
     * @uiName Referral code alignment
     * @uiType string
     * @uiEnum ["left", "center", "right"]
     * @uiEnumNames ["Left", "Center", "Right"]
     * @uiGroup Style
     */
    this.textAlign = "left";
    /**
     * @uiName Copy button label
     */
    this.copyButtonLabel = "Copy Code";
    /**
     * Set the copy button style and placement
     *
     * @uiName Style
     * @uiType string
     * @uiEnum ["icon", "button-outside", "button-below"]
     * @uiEnumNames ["Icon", "Button outside", "Button below"]
     * @uiGroup Style
     */
    this.buttonStyle = "icon";
    /**
     * The type of the button that is used (primary or secondary).
     * @uiName Button Type
     * @uiType string
     * @uiEnum ["primary", "secondary"]
     * @uiEnumNames ["Primary", "Secondary"]
     * @uiGroup Style
     */
    this.buttonType = "primary";
    domContextHooks_module.h$1(this);
  }
  disconnectedCallback() { }
  render() {
    const thisProps = utils.getProps(this);
    const props = index_module.isDemo()
      ? useDemoReferralCode(thisProps)
      : useReferralCode(thisProps);
    return index.h(copyTextView.CopyTextView, Object.assign({}, props));
  }
};
function useDemoReferralCode(props) {
  const [open, setOpen] = domContextHooks_module.useState(false);
  const copyString = "REFERRALCODE001";
  return cjs.cjs({
    copyString: copyString,
    tooltiptext: props.tooltiptext,
    textAlign: props.textAlign,
    copyButtonLabel: props.copyButtonLabel,
    buttonStyle: props.buttonStyle,
    notificationText: props.notificationText,
    showNotificationText: props.showNotificationText,
    isCopied: props.showNotificationText,
    borderColor: props.borderColor,
    backgroundColor: props.backgroundColor,
    textColor: props.textColor,
    borderRadius: props.borderRadius,
    buttonType: props.buttonType,
    isUsed: false,
    rewardStatus: "AVAILABLE",
    open,
    onClick: () => {
      // Should well supported: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard#browser_compatibility
      // Only if called from a user-initiated event
      navigator.clipboard.writeText(copyString);
      setOpen(true);
      setTimeout(() => setOpen(false), props.tooltiplifespan);
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}

function ReferralCodesView(props) {
  const { slots, titleText, states, textColor = "var(--sqm-text-subdued)", } = props;
  const style = {
    Wrapper: {
      width: "100%",
      display: "flex",
      alignItems: "flex-start",
      justifyContent: "center",
      flexDirection: "column",
      gap: "var(--sl-spacing-large)",
      color: textColor,
    },
    HeaderContainer: {
      display: "flex",
      alignItems: "center",
      justifyContent: "space-between",
      width: "100%",
    },
    ShareCodeContainer: {
      width: "100%",
      display: "flex",
      alignItems: "flex-start",
      justifyContent: "center",
      flexDirection: "column",
    },
    ShareButtonContainer: {
      width: "100%",
      display: "flex",
      alignItems: "flex-start",
      justifyContent: "center",
      flexDirection: "column",
    },
    TitleText: {
      fontSize: "var(--sl-font-size-large)",
      margin: "0",
    },
  };
  const vanillaStyle = `
    :host {
      display: block;
    }
    :host([hidden]): {
      display: none;
    }
  `;
  const sheet = JSS.createStyleSheet(style);
  const styleString = sheet.toString();
  const getSlotContent = (states) => {
    if (states.noCodes) {
      return slots.empty;
    }
    if (states.loading) {
      return slots.loading;
    }
    return (index.h("div", { class: sheet.classes.Wrapper },
      index.h("div", { class: sheet.classes.ShareCodeContainer }, slots.shareCodes),
      index.h("div", { class: sheet.classes.ShareButtonContainer }, slots.shareButtons)));
  };
  return (index.h("div", { class: sheet.classes.Wrapper, part: "sqm-base" },
    index.h("style", { type: "text/css" },
      vanillaStyle,
      styleString),
    index.h("div", { class: sheet.classes.HeaderContainer },
      index.h("h2", { class: sheet.classes.TitleText }, titleText),
      !states.noCodes && !states.loading && slots.pagination),
    getSlotContent(states)));
}

const ReferralCodes = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    /**
     * @uiName Title Text
     */
    this.titleText = "Start sharing";
    /**
     * @uiName Empty State Header Text
     */
    this.emptyStateHeaderText = "Your new codes and links aren’t ready yet";
    /**
     * @uiName Empty State Description Text
     */
    this.emptyStateDescriptionText = "Please contact our program support team to let them know you’re out of codes.";
    /**
     * @uiName Empty State Image URL
     */
    this.emptyStateImageUrl = "https://res.cloudinary.com/saasquatch/image/upload/v1644360953/squatch-assets/empty_leaderboard2.png";
    domContextHooks_module.h$1(this);
  }
  disconnectedCallback() { }
  render() {
    const thisProps = utils.getProps(this);
    const props = index_module.isDemo()
      ? useDemoReferralCodes(thisProps)
      : useReferralCodes.useReferralCodes(thisProps);
    const slots = {
      shareButtons: index.h("slot", { name: "shareButtons" }),
      shareCodes: index.h("slot", { name: "shareCodes" }),
      pagination: index.h("slot", { name: "pagination" }),
      empty: (index.h(EmptySlot, { headerText: this.emptyStateHeaderText, descriptionText: this.emptyStateDescriptionText, imageUrl: this.emptyStateImageUrl })),
      loading: index.h(LoadingSlot, null),
    };
    const viewProps = {
      slots,
      ...props,
      loading: props.states.loading,
      noCodes: props.states.noCodes,
    };
    return index.h(ReferralCodesView, Object.assign({}, viewProps));
  }
};
function EmptySlot({ headerText, descriptionText, imageUrl, }) {
  return (index.h("slot", { name: "empty" }, index.h("sqm-empty", { emptyStateHeader: headerText, emptyStateText: descriptionText, emptyStateImage: imageUrl })));
}
function LoadingSlot() {
  return (index.h("slot", { name: "loading" }, index.h(LoadingRow, null), index.h(LoadingRow, null), index.h(LoadingRow, null), index.h(LoadingRow, null)));
}
function LoadingRow() {
  return index.h("sqm-skeleton", null);
}
function useDemoReferralCodes(props) {
  return cjs.cjs({
    titleText: props.titleText,
    textColor: props.textColor,
    states: {
      noCodes: false,
      loading: false,
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}

exports.sqm_pagination = Pagination;
exports.sqm_referral_code = ReferralCode;
exports.sqm_referral_codes = ReferralCodes;
