import { h } from './chunks/d4789960.js';
import './chunks/456564be.js';
import { u as useProp } from './chunks/c26036df.js';
import { u as useEvent } from './chunks/d4058887.js';
import { b as useState, u as useEffect } from './chunks/4ccc6eeb.js';
import { g as getComponentOrDefine } from './chunks/071ed725.js';
import './chunks/23e44913.js';
import './chunks/d91e4e4a.js';
import { O as Option } from './chunks/a02c5734.js';
import { t as toastrMessage } from './chunks/e1e2ec26.js';
import './chunks/d76d6172.js';
import './kun-icon.js';
import './chunks/cd857e99.js';
import './kun-image-or-default.js';
import './chunks/e5702756.js';
import './kun-image.js';
import { g as getKunImageUrl } from './chunks/8c68f334.js';
import './chunks/b7110801.js';
import './chunks/1c3fc91c.js';
import './kun-button-puer.js';
import './kun-emoji-picker.js';
import './kun-input-comment.js';
import { f as formatDate, a as a$1 } from './chunks/29007845.js';
import { e as execWithSixLoad } from './chunks/cf049716.js';
import { r as rn } from './chunks/f484978b.js';

var n=function(){return (n=Object.assign||function(n){for(var t,e=1,r=arguments.length;e<r;e++)for(var o in t=arguments[e])Object.prototype.hasOwnProperty.call(t,o)&&(n[o]=t[o]);return n}).apply(this,arguments)};function t(n,t,e,r){return new(e||(e=Promise))(function(o,i){function a(n){try{c(r.next(n));}catch(n){i(n);}}function l(n){try{c(r.throw(n));}catch(n){i(n);}}function c(n){var t;n.done?o(n.value):(t=n.value,t instanceof e?t:new e(function(n){n(t);})).then(a,l);}c((r=r.apply(n,t||[])).next());})}function e(n,t){var e,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function l(i){return function(l){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;a;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=(o=a.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(n,a);}catch(n){i=[6,n],r=0;}finally{e=o=0;}if(5&i[0])throw i[1];return {value:i[0]?i[1]:void 0,done:!0}}([i,l])}}}var r=window.location.origin,o=""+(r.indexOf("localhost")>-1?"https://www.xunlianying.vip":r);function i(t,e,r){var i={authorization:""+localStorage.getItem("AUTH")},a={authorization:""+localStorage.getItem("AUTH"),"content-type":"application/json"},l={method:"POST",body:!0!==(null==r?void 0:r.isFormData)?JSON.stringify(e):e,headers:!0!==(null==r?void 0:r.isFormData)?n(n({},i),a):i};return fetch(""+o+t,l).then(function(n){return n.json()}).catch(function(n){return n}).then(function(n){var t=n;if("4001"===String(t.code))null!=(null==r?void 0:r.callback)?r.callback():window.location.replace("/web/plume/login");else if("2001"!==t.code)throw new Error(t.msg);return n})}var a=function(n){return t(void 0,void 0,void 0,function(){return e(this,function(t){return [2,i("/comment/v1/50/dianZan",n)]})})};

function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }const style = `
  .kun-item-comment-bg {
    background: #f9f9f9;
  }
  
  .comment-max-width {
    max-width: calc(100% - 60px);
  }
`;

const KunItemComment = () => {
  const [itemProps, setItemProps] = useProp("kunItem");
  const item = Option.of(itemProps).getOrElse({
    postId: "132",
    commentUserName: "蓝小球",
    createAt: "2020-06-07",
    commentId: "xxx",
    niceLookCount: 10,
    content: "加油啊，很不错",
    childComments: {
      records: [{
        createAt: "2020-06-07",
        content: "111",
        parentUserName: "ddd",
        commentUserName: "蓝小球"
      }, {
        createAt: "2020-06-07",
        parentUserName: "ddd",
        content: "111",
        commentUserName: "蓝小球"
      }, {
        parentUserName: "ddd",
        createAt: "2020-06-07",
        content: "111",
        commentUserName: "蓝小球"
      }]
    }
  });
  const [seriesIdProp] = useProp("seriesId");
  const [currentCommentId] = useProp("currentCommentId");
  const [like, setLike] = useState(false);
  const [showResponse, setShowResponse] = useState(false);
  const myRefresh = useEvent("MyRefresh", {
    composed: true,
    bubbles: true
  });
  const toogleCurrentCommentEvent = useEvent("MyChange", {
    composed: true,
    bubbles: true
  });
  useEffect(() => {
    if (item == null) {
      return;
    }

    if (currentCommentId === item.commentId) {
      setShowResponse(true);
    } else {
      setShowResponse(false);
    }
  }, [currentCommentId]);

  const toogleClick = () => {
    if ((item === null || item === void 0 ? void 0 : item.commentId) == null) {
      return;
    }

    setShowResponse(!showResponse);
    toogleCurrentCommentEvent(item.commentId);
  };

  const onCancel = () => {
    setShowResponse(false);
  };

  const onHandleChildComment = async event => {
    const data = {
      postId: item.postId,
      parentCommentId: item.commentId,
      firstCommentId: _nullishCoalesce(item.firstCommentId, () => ( item.commentId)),
      seriesId: seriesIdProp,
      content: event.detail.text
    };
    await execWithSixLoad(async () => {
      await a$1(data);
      myRefresh({});
      setShowResponse(false);
      toastrMessage("评论成功");
    });
  };

  const toogle = async event => {
    await execWithSixLoad(async () => {
      await a({
        feedId: String(item.postId),
        commentId: String(item.commentId),
        isLike: true
      });
      const newData = rn(item, draft => {
        draft.niceLookCount = like ? item.niceLookCount - 1 : item.niceLookCount + 1;
      });
      setItemProps(newData);
      setLike(!like);
    });
    event.preventDefault();
    event.stopPropagation();
  };

  return h("host", null, h("style", null, style), h("div", {
    className: "flex justify-start w-full kun-item-comment-bg py-4 px-4"
  }, h("div", {
    class: "flex-none flex w-16"
  }, h("kun-image-or-default", {
    url: getKunImageUrl(item.commentUserAvatarOpt),
    width: 56,
    height: 56
  })), h("div", {
    class: "flex flex-col px-2 flex-auto relative w-full comment-max-width"
  }, h("div", {
    class: "flex flex-col w-full"
  }, h("div", {
    class: "flex flex-row justify-between w-full"
  }, h("span", {
    class: "flex items-center"
  }, h("span", null, _nullishCoalesce(item.commentUserName, () => ( "用户")))), h("span", {
    className: "ml-2"
  }, formatDate(item.createAt))), h("div", null, h("div", {
    class: "flex flex-row"
  }, h("div", {
    class: "flex justify-start items-center"
  }, !like && h("kun-icon", {
    iconType: "to-dian-zan",
    iconWidth: 18,
    iconHeight: 18,
    onclick: toogle
  }), like && h("kun-icon", {
    iconType: "dianzan",
    iconWidth: 18,
    iconHeight: 18,
    onclick: toogle
  }), item.niceLookCount > 0 && h("span", {
    className: "ml-1"
  }, item.niceLookCount)), h("div", null, h("span", {
    onclick: toogleClick,
    class: "cursor-pointer ml-2"
  }, "\u56DE\u590D"))), h("div", {
    class: "flex-auto flex flex-grow-0"
  }, showResponse && h("kun-input-comment", {
    onMySubmit: onHandleChildComment,
    onMyCancel: onCancel,
    className: "w-full flex flex-auto box-border"
  })), h("div", null, item.childComments.records.map(child => h("kun-child-comment-item-comp", {
    kunItem: child,
    seriesId: seriesIdProp,
    firstCommentId: item.parentCommentId
  }))))))));
};

KunItemComment.props = {
  isPractice: {
    type: Boolean
  },
  currentCommentId: {
    type: String
  },
  seriesId: {
    type: String
  },
  kunItem: {
    type: Object,
    reflect: true
  }
};
var kunItemComment = getComponentOrDefine("kun-item-comment", KunItemComment);

export default kunItemComment;
//# sourceMappingURL=kun-item-comment.js.map
